我使用 gunicorn 和 nginx 配置了一个 django 应用程序,一切都运行良好,直到在服务器上安装 SSL 证书。首先,所有页面都完美呈现,但过了一段时间,一些页面显示 502 错误网关,而其他页面仍然运行良好。
我不是要上传大文件或调用加载时间较长的页面。页面应该立即提供服务。我尝试了所有方法,但找不到问题所在。可能是配置错误。请帮助我
错误发生在 gunicorn 的 error.log 中
[2019-04-20 14:38:24 +0200] [14828] [CRITICAL] WORKER TIMEOUT (pid:21460)
[2019-04-20 12:38:24 +0000] [21460] [INFO] Worker exiting (pid: 21460)
[2019-04-20 14:38:24 +0200] [21500] [INFO] Booting worker with pid: 21500
这是我的 gunicorn 配置
import multiprocessing
timeout = 120
bind = 'unix:/tmp/gunicorn.sock'
workers = multiprocessing.cpu_count() * 2 + 1
reload = True
daemon = True
accesslog = './access.log'
errorlog = './error.log'
nginx 配置
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 1024;
# multi_accept on;
}
http {
fastcgi_buffers 8 16k;
fastcgi_buffer_size 32k;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
##
# 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_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# 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/*;
}
#mail {
# # See sample authentication script at:
# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
# # auth_http localhost/auth.php;
# # pop3_capabilities "TOP" "USER";
# # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
# server {
# listen localhost:110;
# protocol pop3;
# proxy on;
# }
#
# server {
# listen localhost:143;
# protocol imap;
# proxy on;
# }
#}
/etc/nginx/sites-available/示例
upstream your-gunicorn {
server unix:/tmp/gunicorn.sock fail_timeout=0;
}
# Catch all requests with an invalid HOST header
server {
server_name "";
listen 80;
return 444;
}
server {
listen 80;
server_name example.com www.example.com;
return 301 https://www.example.com$request_uri;
}
server {
listen 443 default ssl;
server_name example.com www.example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
client_max_body_size 4G;
keepalive_timeout 70;
access_log /var/log/nginx/example.access_log;
error_log /var/log/nginx/example.error_log warn;
root /var/www/django_projects/example;
location /static/ {
autoindex off;
alias /var/www/django_projects/example/static/;
expires 1M;
access_log off;
add_header Cache-Control "public";
proxy_ignore_headers "Set-Cookie";
}
location @proxy_to_app {
proxy_set_header Host $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://your-gunicorn;
proxy_read_timeout 90;
proxy_redirect http://your-gunicorn https://example.com;
}
location / {
try_files $uri @proxy_to_app;
}
location /.well-known/acme-challenge/ {
root /var/www/django_projects/example/static/;
}
}
nginx 错误日志:
2019/04/21 14:26:28 [error] 7897#7897: *1 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 105.159.137.50, server: example.com, request: "GET /login/ HTTP/1.1", upstream: "http://unix:/tmp/gunicorn.sock/login/", host: $
$host: "www.example.com"
现在我遇到的不是 502 BAd Gateway,而是 504 Gateway Time-out
我的 django 应用程序错误日志中没有任何内容
答案1
我正在使用 django-instagram 从 instagram 个人资料中抓取图片,这就是问题所在。
我删除了 django-instagram 代码,但其运行良好。
但我想知道为什么从 Instagram 抓取数据会出现错误,是因为 nginx 配置吗?因为它在我的本地机器上运行良好