【问题标题】:How to redirect address.com/foo/bar to address.com:port/bar with nginx?如何使用 nginx 将 address.com/foo/bar 重定向到 address.com:port/bar?
【发布时间】:2011-05-25 21:18:47
【问题描述】:

我在我的服务器 ansel.ms 上运行了 nginx,在 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 的东西),我只是从几个来源复制并粘贴了它。

它不起作用。

你能告诉我要改变什么,让它像我上面描述的那样做吗? 谢谢!

【问题讨论】:

    标签: proxy nginx port


    【解决方案1】:

    我无法在您的配置文件中发现错误;我也是 nginx 新手。

    但这是我完整的 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,只有在carabet 后面跟着一个斜线,就像在http://myhost/cabaret/http://myhost/cabaret/foo/bar 中一样。

    【讨论】:

    • 谢谢!您可以使用以下方法解决斜杠问题:server { ... server_name_in_redirect off; ...},并且您必须在您的位置添加一个斜杠,如下所示:location /rhythm/ {...(但不是在重写中!)
    • 这是一种“旁白”,但您是否也在使用 nginx 来记录 POST 请求。我可以很好地记录我的 GET 请求,但是我无法记录来自 POST 请求的正文响应。只是想你可能有一些见解。我的问题发布在:stackoverflow.com/questions/4939382/…
    • 我很高兴在我需要的时候发现了这个问题。为 Anselm Eickhoff 提供额外的 ponus boints。
    • 对我来说,“rewrite”命令使它工作,在“location”指令下
    猜你喜欢
    • 2011-11-14
    • 1970-01-01
    • 1970-01-01
    • 2010-12-09
    • 1970-01-01
    • 2019-08-30
    • 1970-01-01
    • 1970-01-01
    • 2014-03-20
    相关资源
    最近更新 更多