如何为不同的上下文 URI 配置位置指令
How to configure Location directive for different context URI
发生了什么:
我使用以下 nginx.conf 文件进行负载平衡。 Web 应用程序已启动并且 运行 在 nginx 8080 端口上并且能够访问登录页面。但是,当从着陆页移动到 "signup" 页面时,它会抛出错误。
期望值:
nginx 负载均衡器应该将负载重定向到 Location 指令中提到的页面。但这并没有发生。
nginx 文件:
events {
}
http {
upstream 3.121.253.126 {
server 3.121.253.126:8080;
server 3.121.253.126:8080;
server 3.121.253.126:8080;
}
error_log /etc/nginx/error_log.log warn;
client_max_body_size 20m;
proxy_cache_path /etc/nginx/cache keys_zone=one:500m max_size=1000m;
server {
listen 8080;
server_name 3.121.253.126;
root /etc/nginx/html;
index index.html;
location /signup {
root /etc/nginx/html;
index add-user.html;
# proxy_pass http://localhost:8080/signup;
# proxy_set_header Host $host;
# rewrite ^/welcome(.*)$ break;
}
}
}
这是错误日志:
2019/02/21 09:07:42 [error] 6#6: *510 recv() failed (104: Connection
reset by peer) while reading response header from upstream, client:
127.0.0.1, server: 3.121.253.126, request: "GET /signup HTTP/1.0", upstream: "http://127.0.0.1:8080/signup", host: "localhost:8080",
referrer: "http://3.121.253.126:8080/" 2019/02/21 09:07:42 [warn] 6#6:
*510 upstream server temporarily disabled while reading response header from upstream, client: 127.0.0.1, server: 3.121.253.126,
request: "GET /signup HTTP/1.0", upstream:
"http://127.0.0.1:8080/signup", host: "localhost:8080", referrer:
"http://3.121.253.126:8080/" 2019/02/21 09:13:10 [error] 6#6: *1
open() "/etc/nginx/html/signup" failed (2: No such file or directory),
client: 157.33.175.127, server: 3.121.253.126, request: "GET /signup
HTTP/1.1", host: "3.121.253.126:8080", referrer:
"http://3.121.253.126:8080/" 2019/02/21 09:15:57 [error] 6#6: *3
open() "/etc/nginx/html/signup" failed (2: No such file or directory),
client: 157.33.175.127, server: 3.121.253.126, request: "GET /signup
HTTP/1.1", host: "3.121.253.126:8080", referrer:
"http://3.121.253.126:8080/"
根据日志,需要注册 html 文件。但是,我指示它使用 add-user.html 文件。不确定为什么没有发生这种情况。
请推荐
您想将 URI /signup
指向位于 /etc/nginx/html/add-user.html
的文件
有很多方法可以使用 Nginx 实现这一点,包括 rewrite
和 try_files
指令。
例如:
location /signup {
try_files /add-user.html =404;
}
不需要在此 location
块中重复 root
指令,因为它将从周围的块继承相同的值。
=404
什么都不做,因为 add-user.html
总是存在,但是 try_files
需要两个参数。有关详细信息,请参阅 this document。
以上位置将处理任何以 /signup
开头的请求(例如 /signup/
或 /signups
)。
要将其限制为单个 URI /signup
,请使用 =
修饰符。有关详细信息,请参阅 this document。
例如:
location = /signup {
try_files /add-user.html =404;
}
发生了什么:
我使用以下 nginx.conf 文件进行负载平衡。 Web 应用程序已启动并且 运行 在 nginx 8080 端口上并且能够访问登录页面。但是,当从着陆页移动到 "signup" 页面时,它会抛出错误。
期望值:
nginx 负载均衡器应该将负载重定向到 Location 指令中提到的页面。但这并没有发生。
nginx 文件:
events {
}
http {
upstream 3.121.253.126 {
server 3.121.253.126:8080;
server 3.121.253.126:8080;
server 3.121.253.126:8080;
}
error_log /etc/nginx/error_log.log warn;
client_max_body_size 20m;
proxy_cache_path /etc/nginx/cache keys_zone=one:500m max_size=1000m;
server {
listen 8080;
server_name 3.121.253.126;
root /etc/nginx/html;
index index.html;
location /signup {
root /etc/nginx/html;
index add-user.html;
# proxy_pass http://localhost:8080/signup;
# proxy_set_header Host $host;
# rewrite ^/welcome(.*)$ break;
}
}
}
这是错误日志:
2019/02/21 09:07:42 [error] 6#6: *510 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 127.0.0.1, server: 3.121.253.126, request: "GET /signup HTTP/1.0", upstream: "http://127.0.0.1:8080/signup", host: "localhost:8080", referrer: "http://3.121.253.126:8080/" 2019/02/21 09:07:42 [warn] 6#6: *510 upstream server temporarily disabled while reading response header from upstream, client: 127.0.0.1, server: 3.121.253.126, request: "GET /signup HTTP/1.0", upstream: "http://127.0.0.1:8080/signup", host: "localhost:8080", referrer: "http://3.121.253.126:8080/" 2019/02/21 09:13:10 [error] 6#6: *1 open() "/etc/nginx/html/signup" failed (2: No such file or directory), client: 157.33.175.127, server: 3.121.253.126, request: "GET /signup HTTP/1.1", host: "3.121.253.126:8080", referrer: "http://3.121.253.126:8080/" 2019/02/21 09:15:57 [error] 6#6: *3 open() "/etc/nginx/html/signup" failed (2: No such file or directory), client: 157.33.175.127, server: 3.121.253.126, request: "GET /signup HTTP/1.1", host: "3.121.253.126:8080", referrer: "http://3.121.253.126:8080/"
根据日志,需要注册 html 文件。但是,我指示它使用 add-user.html 文件。不确定为什么没有发生这种情况。
请推荐
您想将 URI /signup
指向位于 /etc/nginx/html/add-user.html
有很多方法可以使用 Nginx 实现这一点,包括 rewrite
和 try_files
指令。
例如:
location /signup {
try_files /add-user.html =404;
}
不需要在此 location
块中重复 root
指令,因为它将从周围的块继承相同的值。
=404
什么都不做,因为 add-user.html
总是存在,但是 try_files
需要两个参数。有关详细信息,请参阅 this document。
以上位置将处理任何以 /signup
开头的请求(例如 /signup/
或 /signups
)。
要将其限制为单个 URI /signup
,请使用 =
修饰符。有关详细信息,请参阅 this document。
例如:
location = /signup {
try_files /add-user.html =404;
}