如题

host not found in upstream

1
2
3
4
5
6
7
C:\nginx-1.12.0>nginx -t
nginx: [emerg] host not found in upstream "p.weblb.com:8180" in C:\nginx-1.12.0/conf/../vhosts/crm.web.load.balance.conf:3
nginx: configuration file C:\nginx-1.12.0/conf/nginx.conf test failed

C:\nginx-1.12.0>nginx -t
nginx: [emerg] host not found in upstream "p.rate.com" in C:\nginx-1.12.0/conf/../vhosts/tb.conf:26
nginx: configuration file C:\nginx-1.12.0/conf/nginx.conf test failed
  • 上面两个错误的原因,是域名解析失败,在 hosts 文件添加这两个域名即可

    1
    2
    127.0.0.1 p.weblb.com
    127.0.0.1 p.rate.com
  • 附:配置文件 crm.web.load.balance.conf

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    upstream crm-webs {
    ip_hash;
    server p.weblb.com:8180 weight=5 max_fails=1 fail_timeout=180;
    #server p.javaweb.com:18081 weight=1 max_fails=1 fail_timeout=180;
    #server p.javaweb.com:18082 backup;
    }

    server {
    access_log logs/crm.web.access.log;
    error_log logs/crm.web.error.log;
    listen 81;
    server_name p.weblb.com;

    location / {
    proxy_pass http://crm-webs;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
    }
  • 附:配置文件 tb.conf

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    server {
    listen 80;
    server_name p.tb.com;
    charset utf-8;
    access_log logs/p.tb.com.access.log;
    error_log logs/p.tb.com.error.log;
    root c:/dev.workspace/php/tb/trunk/web/;
    index index.php;

    location ~ \.php$ {
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
    fastcgi_read_timeout 1d; #新增,xdebug调试时长
    }

    location / {
    if (!-e $request_filename) {
    rewrite ^(.*)$ /index.php last;
    break;
    }
    }

    #rate 是另外一个项目,独立部署,这里通过代理访问
    location ^~ /rate {
    proxy_pass http://p.rate.com;
    proxy_redirect http://p.rate.com/ http://d5.51fakd.com/rate/;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

    #crm 是 java web 应用,通过代理直接访问,并且代理做了负载均衡
    location ^~ /crm {
    proxy_pass http://p.weblb.com:8180/crm;
    proxy_redirect http://p.weblb.com:8180/crm/ http://tb.qn.com/crm/;

    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
    }
  • 用 nginx 反向代理达到访问 tomcat 的默认端口应用程序时,域名不加端口 8080,对外就象是使用默认的 80 端口一样

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    server {
    listen 80;
    server_name myjira.com;
    charset utf-8;
    access_log /opt/atlassian/jira/logs/ybdjira.com.access.log;
    error_log /opt/atlassian/jira/logs/ybdjira.com.error.log;

    location / {
    proxy_pass http://localhost:8080;
    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 这句配置是改变 http 的请求头。而 Host 是请求的主机名,X-Real-IP 是请求的真实 IP,X-Forwarded-For 表示请求是由谁发起的。
如果不需要获取真实 IP 的业务时,仅需要如下简单配置即可:

1
2
3
4
5
6
7
8
9
10
11
server {
listen 80;
server_name myjira.com;
charset utf-8;
access_log /opt/atlassian/jira/logs/ybdjira.com.access.log;
error_log /opt/atlassian/jira/logs/ybdjira.com.error.log;

location / {
proxy_pass http://localhost:8080;
}
}