nginx: [emerg] "upstream" directive is not allowed here in .../.../.../*.conf

因为临时需要在本机搭建一个nginx服务使用,其实很简单的一个本地server,但是运行的时候就报错:

192:~ superstar$ nginx
nginx: [emerg] "upstream" directive is not allowed here in /usr/local/nginx/conf/conf.d/test.com.conf:1
192:~ superstar$

检查了我的配置文件 test.com.conf 发现也没有问题:

upstream a_platform{
            server 127.0.0.1:7100 weight=1 max_fails=2 fail_timeout=5s;
}

server {
        listen 80;
        server_name a.test.com;
        access_log   /tmp/nginx/a_platform/access.log; 
        error_log    /tmp/nginx/a_platform/error.log; 

        location ^~ /{
            proxy_pass http://127.0.0.1:7100/;
        }
        location = /{
            proxy_pass http://127.0.0.1:7100/;
        }
}

然后网上查了一下说是“upstream”不能再http这个block里面,于是查看了一下我的nginx.conf的配置;发现果然有问题。

因为是nginx.conf.default改过来的在进行inlude的时候,是直接在server的block里面的,如下红色部分

……………………

events {
    worker_connections  1024;
}

http {
# ……………………其他配置文件……………… server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; }
include …………/conf/conf.d/*.conf; } # ……………………………………其他配置 }

 而实际上应该在黄色server部分以外,如下所示:

……………………

events {
    worker_connections  1024;
}


http {
#  ……………………其他配置文件………………
    server {
        listen       80;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
        }
        
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
       include   …………/conf/conf.d/*.conf;
# ……………………………………其他配置 
}

修改完成,运行,问题解决:

192:~ superstar$ 
192:~ superstar$ nginx
192:~ superstar$ ps -ef | grep nginx
  501 28996     1   0 12:50上午 ??         0:00.00 nginx: master process nginx
  501 28997 28996   0 12:50上午 ??         0:00.00 nginx: worker process
192:~ superstar$

至此问题解决,也吸取一个教训,有时候越简单地事情越要仔细~

原文地址:https://www.cnblogs.com/haojile/p/12387568.html