[英]Nginx redirect to Node.js backend
I want to redirect a URL eg domain.com/api/ to a specific Node.js server, the root URL shows my website.我想将 URL 例如 domain.com/api/ 重定向到特定的 Node.js 服务器,根 URL 显示我的网站。 At the moment I use this config:目前我使用这个配置:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
server_name _;
location / {
try_files $uri $uri/ =404;
}
location /api {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://localhost:3031/;
proxy_ssl_session_reuse off;
proxy_set_header Host $http_host;
proxy_cache_bypass $http_upgrade;
proxy_redirect off;
}
but it does not work.但它不起作用。
What has gone wrong?出了什么问题?
Thanks for help and best regards:)感谢您的帮助和最好的问候:)
You fogot closing bracket after location /api section.您在 location /api 部分之后忘记了右括号。 Your config working at my machine.您的配置在我的机器上工作。
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
server_name _;
location / {
try_files $uri $uri/ =404;
}
location /api {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://localhost:3031/;
proxy_ssl_session_reuse off;
proxy_set_header Host $http_host;
proxy_cache_bypass $http_upgrade;
proxy_redirect off;
}
}
Also your nodejs backend must handle '/api' requests.此外,您的 nodejs 后端必须处理“/api”请求。
Please note that proxy_pass
directive gets argument for backend and optional URI .请注意proxy_pass
指令获取后端和可选 URI的参数。
That means proxy_pass http://localhost:3031;
这意味着proxy_pass http://localhost:3031;
will get the URI from the user, eg /api/res.json
so final URL for the NODE JS is: http://localhost:3031/api/res.json
will get the URI from the user, eg /api/res.json
so final URL for the NODE JS is: http://localhost:3031/api/res.json
But when you provide the URI to the directive itself, it overrides the requested URI in the matching location .但是,当您向指令本身提供 URI 时,它会覆盖匹配 location中请求的 URI。 eg location /api
and proxy_pass http://localhost:3031/;
例如location /api
和proxy_pass http://localhost:3031/;
(note the suffixed slash). (注意后缀的斜杠)。 so the part /api
is replaced by /
and final URL is: http://localhost:3031/res.json
.所以/api
部分被/
替换,最后的 URL 是: http://localhost:3031/res.json
。
from the NGINX docs:来自 NGINX 文档:
If the proxy_pass directive is specified with a URI, then when a request is passed to the server, the part of a normalized request URI matching the location is replaced by a URI specified in the directive:如果 proxy_pass 指令是用一个 URI 指定的,那么当一个请求被传递到服务器时,与该位置匹配的规范化请求 URI 的部分将被指令中指定的 URI 替换:
location /name/ { proxy_pass http://127.0.0.1/remote/; }
So it's important to understand that the part of the requested URI /name/
is replaced by /remote/
and then the rest of the requested URI is added to the final sent URI.因此,重要的是要了解请求的 URI /name/
的部分被/remote/
替换,然后将请求的 URI 的 rest 添加到最终发送的 URI 中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.