node.js应用程序错误的Nginx代理:无法加载资源

时间:2016-05-03 09:39:41

标签: node.js nginx proxy

我有两个node.js应用程序:一个是我的网站,第二个是CI工具。我有以下nginx配置:

server {
    listen 80;

    server_name example.com;

    location / {
        proxy_pass    http://127.0.0.1:8081/;
    }

    location /ci {
        rewrite ^/ci(.*) /$1 break;
        proxy_pass http://127.0.0.1:3000/;
    }
}

当我在浏览器中打开example.com时,它会正确加载index.html请求的所有资源,但是当我打开example.com/ci时,它只加载index.html,我得到了这样的错误:

GET http://example.com/styles/styles.css     404
GET http://example.com/scripts/app.js        404

我已在我的CI应用中将<base href="/ci">添加到index.html的头部,但它没有帮助。

1 个答案:

答案 0 :(得分:0)

我最终得到了下一个nginx配置:

server {
    listen 80;

    server_name example.com;

    location / {
        if ($http_referer ~* example.com/ci) {
            rewrite (.*) http://example.com/ci$1;
        }

        access_log off;

        proxy_pass    http://127.0.0.1:8081/;
        proxy_set_header Host $host;
        proxy_buffering   off;

        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }

    location = /ci { rewrite ^ /ci/ last; }
    location /ci/ {
        access_log off;

        proxy_pass http://127.0.0.1:3000/;
        proxy_set_header Host $host;
        proxy_buffering   off;

        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
}

同样在我的CI应用程序的客户端,我为socket.io连接指定了域和端口:io.connect('http://example.com:3000')。 无需<base>个html标记。