问题(目的):同一服务器下,并存 java 和 node 项目,nginx 实现不同路径转发不同端口 希望的结果是:访问:fashion.goonie.cn/api/wxpay/ 和访问:fashion.goonie.cn:8080/wxpay/ 应该是一样的 我的配置:
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
upstream java{
server fashion.xxxx.cn:8080;
}
upstream node{
server fashion.xxxx.cn:3001;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location ^~ /api/ {
proxy_pass http://java;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Remote_addr $remote_addr;
}
location /* {
proxy_pass http://node;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Remote_addr $remote_addr;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
希望的结果是:访问:fashion.xxxx.cn/api/wxpay/ 和访问:fashion.xxxx.cn:8080/wxpay/ 应该是一样的。 但是现在 fashion.xxxx.cn/api/wxpay/ 访问失败
1
kismetX 2017-12-22 18:03:06 +08:00 1
proxy_pass http://node/
试试这样 |
2
kismetX 2017-12-22 18:09:58 +08:00 1
nginx 里 proxy_pass 加和不加 /有区别的,一看 access_log 便知,配置都没问题,我想你应该已经代理到了 api,但 request_uri 不对
|
3
Beebird 2017-12-22 18:17:05 +08:00 1
location ^~ /api/ {
if ($request_uri ~* "/api/(.*)") { proxy_pass http://java/$1; } proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Remote_addr $remote_addr; } |
4
alvinbone88 2017-12-22 22:53:42 +08:00 1
把 location 里的星号去了
不去也行,以后就通过 /*/example 来访问 API |