如何使用nginx将address.com/foo/bar重定向到address.com:port/bar?
我在我的服务器 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我无法发现您的配置文件中的错误;我也是 nginx 新手。
但这里是我完整的 nginx.conf 配置文件,它将 http://myhost/cabaret/foo/bar 重定向到 http://myhost:8085/foo/bar :
它并不完美,因为它不能与
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
tohttp://myhost:8085/foo/bar
:It is not perfect, because it will not work with
http://myhost/cabaret
, only if a slash followscarabet
like inhttp://myhost/cabaret/
orhttp://myhost/cabaret/foo/bar
.