Nginx 没有指向 gunicorn 套接字,returns 404 Not Found

Nginx not directing to gunicorn socket, returns 404 Not Found

我似乎无法让 nginx 重定向到我的 gunicorn 套接字。我尝试了 Whosebug 的许多解决方案,但代码“看起来”是正确的。 尝试访问 https://example.com

时一直给我 404 页面

这是 gunicorn 套接字状态,位于 /var/www/example.com/example_app.sock:

example_app.service - uWSGI instance to serve the app "example.com"
     Loaded: loaded (/etc/systemd/system/example_app.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2020-09-29 16:08:45 CST; 26min ago
   Main PID: 154344 (gunicorn)
      Tasks: 3 (limit: 9522)
     Memory: 87.1M
     CGroup: /system.slice/example_app.service
             ├─154344 /usr/bin/python3 /usr/local/bin/gunicorn --workers 1 --bind unix:example_app.sock -m 007 run:app
             └─154363 /usr/bin/python3 /usr/local/bin/gunicorn --workers 1 --bind unix:example_app.sock -m 007 run:app

Sep 29 16:08:45 azompr1 systemd[1]: Started uWSGI instance to serve the app "example.com".
Sep 29 16:08:46 azompr1 gunicorn[154344]: [2020-09-29 16:08:46 +0800] [154344] [INFO] Starting gunicorn 20.0.4
Sep 29 16:08:46 azompr1 gunicorn[154344]: [2020-09-29 16:08:46 +0800] [154344] [INFO] Listening at: unix:example_app.sock (154344)
Sep 29 16:08:46 azompr1 gunicorn[154344]: [2020-09-29 16:08:46 +0800] [154344] [INFO] Using worker: sync
Sep 29 16:08:46 azompr1 gunicorn[154363]: [2020-09-29 16:08:46 +0800] [154363] [INFO] Booting worker with pid: 154363

example_app.service代码,位于/etc/systemd/system/example_app.service:

[Unit]
Description=uWSGI instance to serve the app "example.com"
After=network.target

[Service]
User=www-data
Group=www-data
WorkingDirectory=/var/www/example.com
Environment="PATH=/var/www/example.com/env/bin"
#ExecStart=/var/www/example.com/env/bin/gunicorn --workers 2 --bind unix:example_app.sock -m 007 run:app
ExecStart=gunicorn --workers 1  --bind unix:example_app.sock -m 007  run:app

[Install]
WantedBy=multi-user.target

nginx 设置,/etc/nginx/sites-available/example.com

server {

    return 301 https://$host$request_uri;


    listen 80;
    server_name example.com;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /var/www/example.com;
    }

    location / {
    include proxy_params;
    proxy_pass http://unix:/var/www/example.com/example_app.sock;
   }
}

我找到了原因,这个域的相关ssl证书是同一服务器上另一个证书的一部分,我认为如果配置正确应该不会造成大麻烦。

这个警告让我想到了:

nginx: [warn] conflicting server name "example.com" on 0.0.0.0:443, ignored

我检查了所有这些以了解发生了什么:

sudo less /var/log/nginx/error.log: the Nginx error logs.
sudo less /var/log/nginx/access.log: the Nginx access logs.
sudo journalctl -u nginx: the Nginx process logs.
sudo journalctl -u example.com: Flask app’s Gunicorn logs.

但我所做的解决方案是使用此命令通过更新证书将其从其他域中删除,我从列表中删除了我的:

sudo certbot --cert-name xxxx.org -d xxxxx.com -d yyyy.org -d yyyy.com

然后 re-created 它用于此域。

sudo certbot --nginx -d example.com -d example.co -d www.example.com

最终的 nginx 配置是这样的:

server {
    server_name example.com www.example.com example.co;
    access_log     /var/log/nginx/domains/main.example.com.log;
    error_log  /var/log/nginx/domains/main.example.com.error.log error;

    location / {
        include proxy_params;
        proxy_pass http://unix:/var/www/example.com/azom_care.sock;
    }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}
server {
    if ($host = www.example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = example.co) {
        return 301 https://$host$request_uri;                                                                                                                                                                          } # managed by Certbot                                                                                                                                                                                                                                                                                                                                                                                                            
    if ($host = example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen 80;
    server_name example.com www.example.com example.co;
    return 404; # managed by Certbot

}

它工作正常 Alhamdullellah!