【问题标题】:Nginx + Systemd/Systemctl + Django not working (502 bad gateway)Nginx + Systemd/Systemctl + Django 不工作(502 网关错误)
【发布时间】:2016-12-31 04:37:33
【问题描述】:

我已经为此工作了好几个小时,但我无法弄清楚为什么我的设置不起作用。如果我自己运行 exec 中的命令,我可以通过浏览器访问该页面。但是当我尝试将它作为服务运行时,我得到一个 502: Bad Gateway。

我第一次尝试使用Unix套接字,但是当它不起作用时,我只是直接插入IP和端口,仍然没有运气。

gunicorn.service:

[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=username
WorkingDirectory=/home/username/naomiselect
Environment="PATH=/home/username/naomiselect/naomienv/bin"
ExecStart=/home/username/naomiselect/naomienv/bin/gunicorn --workers 3 --bind local_ip:8000 naomiselect.wsgi:application

[Install]
WantedBy=multi-user.target

nginx.conf:

user www-data;
worker_processes auto;
pid /run/nginx.pid;

events {
    worker_connections 768;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # SSL Settings
    ##

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

网站可用/naomiselect:

server {
    listen 80;
    server_name local_ip;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/username/naomiselect;
    }

    location / {
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_pass http://local_ip:8000/;
    }
}

目录组和所有者分别是www-data和username,递归,权限为drwxrwxr-x。

当我参考了几个教程并尝试了不同的方法并且没有一个起作用时,我不知道自己做错了什么。

【问题讨论】:

  • 502 响应意味着上游服务器无法连接。你确定 gunicorn 正在运行吗?
  • 我认为它根本没有运行,不。状态表示服务的“已退出”而不是“正在运行”。不知道为什么它会退出。
  • journalctl -xe 中有什么关于它退出的原因吗?
  • 不,只是说它已经开始了。没有什么出口,这很奇怪。
  • 我对 gunicorn 不是很熟悉,但是在某处是否有日志文件,可能在 /var/log 中?

标签: django nginx systemd systemctl


【解决方案1】:

所以您说(在 cmets 中)gunicorn.service 的状态在您启动后显示为“已退出”而不是“正在运行”。

大多数时候,这意味着服务Type= 配置错误。默认值为Type=simple,它需要一个非分叉进程。很可能,您的守护进程在启动后分叉到后台,这让 systemd 感到困惑:

如果设置为simple(如果既不是Type=也不是BusName=,则为默认值,但 ExecStart=是指定的),预计配置的进程 with ExecStart= 是服务的主进程。

如果设置为forking,则预计进程配置为 ExecStart= 将调用 fork() 作为其启动的一部分。父母 当启动完成并且所有 沟通渠道已建立。

所以,要检查这个假设,请从 shell 运行守护程序二进制文件。如果它立即释放 shell,那么你的守护进程正在“分叉”,你需要在你的单元文件中传递相应的选项:

[Service]
Type=forking

【讨论】:

    猜你喜欢
    • 2020-09-29
    • 2012-07-16
    • 2012-05-28
    • 2018-12-05
    • 2017-07-07
    • 2020-04-05
    • 2019-01-01
    • 2015-04-22
    • 2021-11-19
    相关资源
    最近更新 更多