ubuntu 系统中配置nginx:
我做的是基于IP地址虚拟主机配置。 当在往往访问时,第一个location对应的服务可以被访问到,第二location对应的服务不能被访问到。
求大神帮帮忙?
server {
listen 80;
server_name aaaa.bbbb.ccc.ddd; // my ip address
location / {
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:80;
}
location /node/ {
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http//:127.0.0.1:9000;
}
}
- 顺序调整一下
- location 不同写法的优先级不同
示例:
# HTTP server
#
server {
listen 80;
server_name domain.name localhost;
charset utf-8;
#access_log /var/log/nginx/nginx.access.log main;
add_header X-Cache $upstream_cache_status;
location /api {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
# proxy_set_header X-NginX-Proxy true;
proxy_redirect off;
proxy_pass http://127.0.0.1:8080;
}
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
# proxy_set_header X-NginX-Proxy true;
proxy_redirect off;
proxy_pass http://127.0.0.1:3000;
}
}
看官方的解释:
To find location matching a given request, nginx first checks locations defined using the prefix strings (prefix locations). Among them, the location with the longest matching prefix is selected and remembered.
你的配置应该没有问题,不知道你如何访问的。
@thonatos 我更改了顺序,同样没有效果。当我使用一个location时,请求服务http://aaaa.bbbb.ccc.ddd/node时,依旧请求不到服务。
server {
listen 80;
server_name aaaa.bbbb.ccc.ddd; // my ip address
location /node/ {
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:9000;
}
}
@thonatos 我更改了顺序,同样没有效果。当我使用一个location时,请求服务http://aaaa.bbbb.ccc.ddd/node时,依旧请求不到服务。
server {
listen 80;
server_name aaaa.bbbb.ccc.ddd; // my ip address
location /node/ {
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:9000;
}
}
@jiangli373 我在服务器配置的就是 proxy_pass http://127.0.0.1:9000; 还是不行哦
location /node/ {
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http//:127.0.0.1:9000;
}
你的这个location /node/
后面有个斜杠,但是你访问的时候没有添加斜杠http://aaaa.bbbb.ccc.ddd/node
要不你访问的地址添加个/
,要不的你的location
/node/
后面的斜杠去掉