我正在使用Express在我的nodejs应用程序上提供静态文件和动态URL。我在Mongodb中基于:id生成URL,然后在NGINX上使用Nodejs反向代理将此路径提供给用户,但是无论我如何尝试都行不通。它总是说“无法获取..”
我正在运行NGINX 1.14.2,节点8.14.0。我已经尝试了NGINX重写指令和各种通配符(〜,〜^ 、. 等),但是它们都不起作用。我在下面包括了当前的快速代码和NGINX配置。任何帮助都将不胜感激
快递
router.get('/dynamic-uri/:fooId', function (req, res) {
res.render('index');
});
带有:fooId的结果URL是
https://example.com/myNodeApp/dynamic-uri/5c35e51228122504b57126d7
NGINX配置
location /myNodeApp/dynamic-uri {
proxy_pass http://x.x.x.x:3000;
proxy_http_version 1.1;
proxy_redirect off;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_set_header X-NginX-Proxy true;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Real-IP $remote_addr;
}
当然:fooId不作为静态路径存在,并且要提供静态文件,我有以下工作正常的块
location ~* /myNodeApp/(static|img|media|app|css|js) {
proxy_pass http://x.x.x.x:3000;
proxy_http_version 1.1;
proxy_redirect off;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
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_cache_bypass $http_upgrade;
proxy_set_header X-Real-IP $remote_addr;
}