Nginx未指向gunicorn套接字,返回404未找到

2024-06-26 14:30:41 发布

您现在位置:Python中文网/ 问答频道 /正文

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

enter image description here

以下是位于/var/www/example.com/example_app.sock的gunicorn套接字状态:

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

示例_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;
   }
}

Tags: comappbinbindexamplevarusrwww
1条回答
网友
1楼 · 发布于 2024-06-26 14:30:41

我找到了原因,这个域的相关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

然后为这个域重新创建它

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

}

而且它工作正常,阿罕杜莱拉

相关问题 更多 >