代理 – 如何使用nginx将address.com/foo/bar重定向到address.com:port/bar?

我在我的服务器上运行nginx ansel.ms和ansel.ms:46156上的一个node.js应用程序.

我想设置nginx,所以它重定向所有的东西

ansel.ms/rhythm

ansel.ms:46156.


ansel.ms/rhythm/sub/path

应该成为

ansel.ms:46156/sub/path

这是我的文件在网站可用:

upstream rhythm {
    server ansel.ms:46156;
}

server {
    listen   80;
    server_name ansel.ms www.ansel.ms;
    access_log /srv/www/ansel.ms/logs/access.log;
    error_log /srv/www/ansel.ms/logs/error.log;

    location / {
        root   /srv/www/ansel.ms/public_html;
        index  index.html index.htm;
    }

    location ~ \.php${
        include fastcgi_params;
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param  SCRIPT_FILENAME  /srv/www/ansel.ms/public_html$fastcgi_script_name;
    }

    location /rhythm{
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;

        proxy_pass http://rhythm;
        proxy_redirect off;
    }
}

我真的不明白这是做什么(proxy_set_header的东西),我只是复制&从几个来源粘贴它.

它不工作

你能给我一个提示什么改变,所以这是我上面描述的?
谢谢!
最佳答案
我无法发现您的配置文件中的错误;我也是一个nginx-newbie.

但是这里是我的完整的nginx.conf配置文件,它将http:// myhost / cabaret / foo / bar重定向到http:// myhost:8085 / foo / bar:

user www-data;
worker_processes  1;
error_log  /var/log/nginx/error.log;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include     /etc/nginx/mime.types;
    access_log  /var/log/nginx/access.log;
    sendfile        on;
    keepalive_timeout  65;
    tcp_nodelay        on;

    server {

        listen   *:80; ## listen for ipv4
        access_log  /var/log/nginx/localhost.access.log;
        location /cabaret {
               rewrite /cabaret(.*) $1 break;
               proxy_pass http://127.0.0.1:8085;
               proxy_redirect     off;
               proxy_set_header   Host             $host;
               proxy_set_header   X-Real-IP        $remote_addr;
               proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        }
    }
}

这不是完美的,因为它不会与http:// myhost / cabaret一起工作,只有如果在如下http:// myhost / cabaret /或http:// myhost / cabaret / foo / bar之后的一个斜杠.

dawei

【声明】:淮南站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。