大网数据广告活动
服务器运维问答

Nginx reverse proxy trailing slash

· 52CE 我爱测

This question is already answered multiple times but in my case there is a twist. I am having a node app which is running on port 3000. I am using nginx to reverse proxy. Now the application only works if there is a slash in the …

问题原文

原帖链接 · Minato Namikaze · CC BY-SA 3.0

This question is already answered multiple times but in my case there is a twist.

I am having a node app which is running on port 3000. I am using nginx to reverse proxy. Now the application only works if there is a slash in the end.

So this works: https://example.com/main/site/react/

This does not: https://example.com/main/site/react

Here is the configuration which I am using:

location /main/site/react {
    error_log /var/log/nginx/app.error.log;
    rewrite ^/main/site/react/(?:|[\w\/]+)(\/static.*)$ $1 break;
    rewrite ^/main/site/react/?(.*)$ /$1 break;
    proxy_pass http://127.0.0.1:3000/;
}

After checking other related questions, adding following rewrite rule(after error_log) worked for me:

rewrite ^(.*[^/])$ $1/ permanent;

Above rule rendered my node application, but it failed with my static resources. I have some static resources rewrites which are not working.

So this URL is not working: https://example.com/main/site/react/static/js/bundle.js. It always renders index HTML page.

已采纳解答

原帖链接 · Niraj Chauhan · CC BY-SA 3.0

Your config looks good. Just add the rewrite at the end:

location /main/site/react {
    rewrite ^/main/site/react$ https://$host/main/site/react/ permanent;
    rewrite ^/main/site/react/(?:|[\w\/]+)(\/static.*)$ $1 break;
    rewrite ^/main/site/react/?(.*)$ /$1 break;
    proxy_pass http://127.0.0.1:3000;
}

Also have modified regex which will handle slash before query params.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-05-22)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。