我正在将Nginx配置为内部Gunicorn服务器的面向公众的代理服务器,以托管我正在开发的“ Reddit clone” Flask项目。有一次,Nginx正常工作(当我使用与在线教程基本相同的配置时),但是在进行适合我的应用程序的更新后,导航到我的Amazon Lightsail时出现“内部服务器错误”( Ubuntu 16.04)服务器的IP地址,并将更改恢复为教程配置现在不起作用。
我尝试过:
1.停止并启动Nginx服务
2.运行sudo netstat -tulpn
,找到PID(对于本地地址0.0.0.0:80
和0.0.0.0:443
似乎出现两次),用sudo fuser -k 80/tcp
和sudo fuser -k 443/tcp
终止进程,然后然后再次启动Nginx
3.从我的系统中完全删除Nginx并重新安装:
sudo apt-get purge --auto-remove nginx
sudo apt-get -y install nginx
flask_reddit
(我在/etc/nginx/sites-enabled/
中的配置文件):
# write access and error logs to /var/log
access_log /var/log/nginx/flask_reddit/flask-reddit_access.log;
error_log /var/log/nginx/flask_reddit/flask-reddit_error.log;
server {
# As Gunicorn documentation states, prevent host spoofing by blocking requests without "Host" request header set
listen 80;
listen 443;
server_name "";
return 444;
}
server {
# listen on port 80 (http)
listen 80 default_server;
server_name _;
location / {
# redirect any requests to the same URL but on https
return 301 https://$host$request_uri;
}
}
server {
# listen on port 443 (https)
listen 443 ssl default_server;
server_name _;
client_max_body_size 5m; # Useful for situations such as file uploads; will return 413 code in violation of this limit
keepalive_timeout 120s 120s; # Used to expedite request processing
# location of the self-signed SSL certificate
ssl_certificate /home/ubuntu/flask-reddit/certs/cert.pem;
ssl_certificate_key /home/ubuntu/flask-reddit/certs/key.pem;
location / {
# forward application requests to the gunicorn server
proxy_pass http://localhost:8000;
proxy_redirect off; # Preserve the fact that Gunicorn handled the request by disabling proxy_pass->location URL prefix change
proxy_set_header Host $host; # When a domain name is configured, this will equal the name in lowercase with no port (protocol added in X-Forwarded-Proto)
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;
}
location /static {
# handle static files directly, without forwarding to the application
root /home/ubuntu/flask-reddit/app;
try_files $uri /templates/404.html; # Provide custom-written 404 response page
expires 30d;
}
}
运行sudo service nginx status
时,得到以下输出:
● nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Drop-In: /etc/systemd/system/nginx.service.d
└─override.conf
Active: active (running) since Thu 2019-08-29 04:07:42 UTC; 22h ago
Process: 4744 ExecStopPost=/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
Process: 4741 ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid (code=exited, status=1/FAILURE)
Process: 5678 ExecReload=/usr/sbin/nginx -g daemon on; master_process on; -s reload (code=exited, status=0/SUCCESS)
Process: 4856 ExecStartPost=/bin/sleep 0.1 (code=exited, status=0/SUCCESS)
Process: 4852 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Process: 4849 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Main PID: 4855 (nginx)
Tasks: 2
Memory: 2.7M
CPU: 309ms
CGroup: /system.slice/nginx.service
├─4855 nginx: master process /usr/sbin/nginx -g daemon on; master_process on
└─5681 nginx: worker process
Warning: Journal has been rotated since unit was started. Log output is incomplete or unavailable.
如过去所见,如果我导航到服务器的公共IP地址,我很难弄清是什么阻碍了Nginx为我的应用程序提供服务,并且可以使用一些帮助。