开发者社区> 问答> 正文

nginx设置反向代理访问报错?报错

  1. 本机(记作主机A)安装了Windows版本的nginx,配置文件内容如下:
    worker_processes  4;
    #pid        logs/nginx.pid;
    events 
    {
        worker_connections  1024;
    }
    http
    {
        include       mime.types;
        default_type  application/octet-stream;
        sendfile        on;
        keepalive_timeout  65;
        upstream server2
        {
            server a4.chinaunix  weight=1 max_fails=2 fail_timeout=30;
        }
        server 
        {
              listen       80;
              server_name  127.0.0.1;
         location /A4Hello
         {
                  proxy_pass  http://server2;
                  client_max_body_size    100m;      
             client_body_buffer_size 128k;             
             proxy_send_timeout   300;             
             proxy_read_timeout   300;             
             proxy_buffer_size    4k;             
             proxy_buffers     16 32k;             
             proxy_busy_buffers_size 64k;             
             proxy_temp_file_write_size 64k;             
             proxy_connect_timeout 30s;                     
             proxy_set_header   Host   $host;             
             proxy_set_header   X-Real-IP  $remote_addr;             
             proxy_set_header   X-Forwarded-For    $proxy_add_x_forwarded_for;             
             }
            #error_page  404              /404.html;
            # redirect server error pages to the static page /50x.html
            error_page   500 502 503 504  /50x.html;
            location = /50x.html 
            {
                root   html;
            }
        }
    }


  2. 域名a4.chinaunix对应2个IP,分别是10.1.1.1和10.1.1.2,该机器记作B;
  3. 想从主机A上访问主机B上的IIS站点A4Hello
       (1)在主机A的HOSTS文件中配置了10.1.1.1  a4.chinaunix
       (2)在主机A的浏览器中输入http://localhost/A4Hello,报错:
       An error occurred.


    Sorry, the page you are looking for is currently unavailable.
     Please try again later.


    If you are the system administrator of this resource then you should check the error log for details.


    Faithfully yours, nginx.


    查看日志中显示:2016/10/11 14:32:26 [error] 3212#2404: *57 upstream timed out (10060: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond) while connecting to upstream, client: 127.0.0.1, server: 127.0.0.1, request: "GET /A4Portal/ HTTP/1.1", upstream: "http://10.1.1.1:80/A4Hello/", host: "localhost"
    请问是nginx配置文件哪些错了呢?谢谢!




        




展开
收起
爱吃鱼的程序员 2020-06-08 21:25:21 1111 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    有没有试过改成这样:

    upstreamserver2
      {
         server10.1.1.1 weight=1max_fails=2fail_timeout=30;

           server10.1.1.2 weight=1max_fails=2fail_timeout=30;

     }

    回复 @Jamie-Liu:嗯,你说的没错!我找到原因了。就是后端主机B设置了防火墙,限制了主机A的访问。添加防火墙允许就可以了。回复 @波克比:我建了三台虚拟机,然后把你的nginx配置原模原样拷贝下来放在nginx机器上,另外两台做web服务,就只修改了这一段,就能成功访问。至于你访问提示的错误,显示的是超时,这种情况可能是因为upstream里面的服务器地址写错了,找不到后端主机;也可能是后端主机80端口访问受限制,或者后端主机上的A4Hello目录权限不允许apache/www用户访问。试过了,也不行。(>_<)那不写着连接到upstream的时候超时了,机器B有没有做配置,也就是说直接用域名访问机器B是否可用按你说的改完试了下,还是不行!(O_O)回复 @波克比:感觉你这个配置有点绕,这么试一下upstreamserver2{server10.1.1.1...;server10.1.1.2...;}server_name域名;hosts文件域名指向127.0.0.1然后使用http://域名来访问但是我用另外一台主机C,访问主机B,输入同样的地址就能访问!不晓得是不是我本机A做了什么限制!o(≧口≦)o  upstreamserver2{
        server10.1.1.1weight=1max_fails=2fail_timeout=30;
        server10.1.1.2weight=1max_fails=2fail_timeout=30;
            }
      server{
        listen   80;
        server_namea4.chinaunix;
    2020-06-08 21:25:39
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
《Nginx 代理系统常用手册》 立即下载
CentOS Nginx PHP JAVA 多语言镜像使用手 立即下载
CentOS Nginx PHP JAVA多语言镜像使用手册 立即下载