因為臨時需要在本機搭建一個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$
至此問題解決,也吸取一個教訓,有時候越簡單地事情越要仔細~