如何为不同的上下文URI配置位置指令

时间:2019-02-21 10:33:59

标签: nginx nginx-config

正在发生的事情:

我使用以下nginx.conf文件进行负载平衡。 Web应用程序已启动并在nginx 8080端口上运行,并且能够访问登录页面。但是,从登录页面转到“注册”页面时,它会抛出错误。

预期结果:

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(.*)$ $1 break;
    }

  }

}

这是错误日志:

  

2019/02/21 09:07:42 [错误] 6#6:* 510 recv()失败(104:连接   从对等方重置),同时从上游客户端读取响应头:   127.0.0.1,服务器:3.121.253.126,请求:“ GET / signup HTTP / 1.0”,上游:“ http://127.0.0.1:8080/signup”,主机:“ localhost:8080”,   推荐人:“ http://3.121.253.126:8080/” 2019/02/21 09:07:42 [warn] 6#6:   * 510上游服务器暂时禁用,同时从上游读取响应头,客户端:127.0.0.1,服务器:3.121.253.126,   请求:“ GET / signup HTTP / 1.0”,上游:   “ http://127.0.0.1:8080/signup”,主机:“ localhost:8080”,引荐来源网址:   “ http://3.121.253.126:8080/” 2019/02/21 09:13:10 [错误] 6#6:* 1   open()“ / etc / nginx / html / signup”失败(2:无此类文件或目录),   客户端:157.33.175.127,服务器:3.121.253.126,请求:“ GET / signup   HTTP / 1.1”,主机:“ 3.121.253.126:8080”,引荐来源网址:   “ http://3.121.253.126:8080/” 2019/02/21 09:15:57 [错误] 6#6:* 3   open()“ / etc / nginx / html / signup”失败(2:无此类文件或目录),   客户端:157.33.175.127,服务器:3.121.253.126,请求:“ GET / signup   HTTP / 1.1”,主机:“ 3.121.253.126:8080”,引荐来源网址:   “ http://3.121.253.126:8080/

根据日志,它需要注册html文件。但是,我指示它使用add-user.html文件。不知道为什么不会这样。

请提出建议

1 个答案:

答案 0 :(得分:1)

您想将URI /signup指向位于/etc/nginx/html/add-user.html的文件

使用Nginx可以实现多种方法,包括rewritetry_files指令。

例如:

location /signup {
    try_files /add-user.html =404;
}

root指令无需在此location块内重复,因为它将从周围的块继承相同的值。

=404不执行任何操作,因为add-user.html始终存在,但是try_files需要两个参数。有关详细信息,请参见this document

上述位置将处理任何以/signup开头的请求(例如/signup//signups)。

要将其限制为单个URI /signup,请使用=修饰符。有关详细信息,请参见this document

例如:

location = /signup {
    try_files /add-user.html =404;
}
相关问题