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

发布于 2024-10-08 09:08:27 字数 1296 浏览 2 评论 0原文

我在我的服务器 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 的东西),我只复制了 & 。从多个来源粘贴它。

这不起作用。

你能给我一个提示,让我改变什么,让它达到我上面描述的效果吗? 谢谢你!

I have running nginx on my server ansel.ms and a node.js app on ansel.ms:46156.

I want to setup nginx so it redirects everything from

ansel.ms/rhythm

to

ansel.ms:46156.


ansel.ms/rhythm/sub/path

should become

ansel.ms:46156/sub/path

This is my file in sites-available:

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;
    }
}

I do not really understand deeply what this does (the proxy_set_header stuff), I only copied & pasted it from several sources.

It doesn't work.

Can you give me a hint what to change so it does what I described above?
Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

奈何桥上唱咆哮 2024-10-15 09:08:27

我无法发现您的配置文件中的错误;我也是 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

I cannot spot the error in your configuration file; I'm an nginx-newbie as well.

But here is my full nginx.conf config file which redirects http://myhost/cabaret/foo/bar to 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;
        }
    }
}

It is not perfect, because it will not work with http://myhost/cabaret, only if a slash follows carabet like in http://myhost/cabaret/ or http://myhost/cabaret/foo/bar.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文