Centos

nginx 失敗 (13: Permission denied) uwsgi (502 bad gateway)

  • February 13, 2018

我無法在新的 DigitalOcean 液滴上執行我的應用程序。本機執行 CentOS 6.5

我的/etc/nginx/nginx.conf

user  nginx www-data;
worker_processes  1;

error_log  /var/log/nginx/error.log;
#error_log  /var/log/nginx/error.log  notice;
#error_log  /var/log/nginx/error.log  info;

pid        /var/run/nginx.pid;
(continues)

我的myapp_nginx.conf

server {

  server_name 104.xxx.x.xxx;
   listen      8080;
   charset     utf-8;
   client_max_body_size 30M;

   location / {
       index index.html index.htm;
       root /home/webdev/mydevelopment/git/ers_portal;
       try_files $uri @app;
   }

   location /static {
   alias /home/webdev/mydevelopment/git/ers_portal/app/static;
   }

   location @app {
       include uwsgi_params;
       uwsgi_pass unix:/home/webdev/mydevelopment/git/ers_portal_uwsgi.sock;
   }
}

我的myapp_uwsgi.ini

[uwsgi]
master = true
#user info
uid = webdev
gid = www-data

#application's base folder
base = /home/webdev/mydevelopment/git/ers_portal

#chdir to this folder when starting
#chdir = /home/webdev/mydevelopment/git/ers_portal

#python module to import
app = run_web
module = %(app)

home = /home/webdev/mydevelopment/venvs/ecodev_do
pythonpath = %(base)

#socket file's location
socket = /home/webdev/mydevelopment/git/ers_portal/%n.sock

#permissions for the socket file
chmod-socket    = 666

#the variable that holds a flask application inside the module imported at line #6
#uwsgi varible only, does not relate to your flask application
callable = app

#location of log files
logto = /home/webdev/mydevelopment/git/ers_portal/logs/%n.log

每當error.log我向104.xxx.x.xxx:8080

2014/11/19 23:14:47 [crit] 28090#0: *1 stat() "/home/webdev/mydevelopment/git/ers_portal/favicon.ico" failed (13: Permission denied), client: 71.211.xxx.xxx, server: 104.xxx.x.xxx, request: "GET /favicon.ico HTTP/1.1", host: "104.xxx.x.xxx:8080"
2014/11/19 23:14:47 [crit] 28090#0: *1 connect() to unix:/home/webdev/mydevelopment/git/ers_portal_uwsgi.sock failed (13: Permission denied) while connecting to upstream, client: 71.211.xxx.xxx, server: 104.xxx.x.xxx, request: "GET /favicon.ico HTTP/1.1", upstream: "uwsgi://unix:/home/webdev/mydevelopment/git/ers_portal_uwsgi.sock:", host: "104.xxx.x.xxx:8080"

項目文件夾:drwxr-xr-x 6 webdev www-data 4.0K Nov 19 23:40 ers_portal

該文件夾內的套接字:srw-rw-rw- 1 webdev www-data 0 Nov 19 23:45 ers_portal_uwsgi.sock

如果您有任何其他資訊需要,請告訴我。謝謝!!

編輯

我按照@Eugene 的建議更改了權限。現在在我的/var/log/nginx/access.log我看到

[20/Nov/2014:01:37:58 -0500] "GET / HTTP/1.1" 502 574 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36" "-"

/var/log/nginx/error.log我看到

2014/11/20 01:38:27 [crit] 28639#0: *1 connect() to unix:/home/webdev/mydevelopment/git/ers_portal_uwsgi.sock failed (2: No such file or directory) while connecting to upstream, client: 71.21.my.local.ip, server: , request: "GET / HTTP/1.1", upstream: "uwsgi://unix:/home/webdev/mydevelopment/git/ers_portal_uwsgi.sock:", host: "104.ser.ver.ip"

favicon.ico 拒絕(下)var/log/nginx/access.log不再出現

"GET /favicon.ico HTTP/1.1" 502 574 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36" "-"

解決了

經過幾個小時的搜尋,我發現了我的錯字。在/path/to/my/webapp/my_app_nginx.conf讀取的行中

location @app {
       include uwsgi_params;
       uwsgi_pass unix:/home/webdev/mydevelopment/git/ers_portal_uwsgi.sock;
   }

應該讀

location @app {
       include uwsgi_params;
       uwsgi_pass unix:/home/webdev/mydevelopment/git/ers_portal/ers_portal_uwsgi.sock;
   }

此外,在/etc/nginx/nginx.conf更改user nginx www-data;user nginx;

還要確保“頂級”/etc/nginx/nginx.conf中沒有與您的應用程序配置衝突的預設伺服器塊(在我的情況下/path/to/my/webapp/my_app_nginx.conf,它與/etc/nginx/conf.d/文件夾的符號連結)。

得到教訓…

  1. 不要氣餒。世界上最糟糕的(也許是第二糟糕的)感覺是無法在很長一段時間內弄清楚某些事情。特別是如果這是你的愛好,你很容易覺得自己無能,這東西太難了,你還不夠好。堅持下去。
  2. 有條不紊。我變得瘋狂改變,尤其是在最後。這會導致上床睡覺並沒有真正的進展,然後在原來的地方醒來。把它全部寫下來,記錄你嘗試過的東西並跟踪以前的變化。ifABABA是工作配置,並且---B-是您正在尋找的更改,但您忘記更改--B----A--,您將重複並重複自己。見#1
  3. 請記住,有時要找到解決方案,最重要的是站起來走開。
  4. 不要低估你的同行(這個社區)的知識。雖然他們可能無法為您的特定問題提供即時答案,但請記住*,我們所有人都不像我們沒有人那麼愚蠢*(或類似的愚蠢說法)。我們都對從地球上清除問題有著根深蒂固的熱愛,一次一個。

引用自:https://serverfault.com/questions/645698