如何解决 nginx 内部服务器错误

如何解决 nginx 内部服务器错误

我正在将 Nginx 配置为面向公众的代理服务器,以托管我正在开发的“Reddit 克隆”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/):

server {
    # As Gunicorn documentation states, prevent host spoofing by blocking requests without "Host" request header set
#    access_log /var/log/nginx/flask_reddit/flask-reddit_access.log;
#    error_log /var/log/nginx/flask_reddit/flask-reddit_error.log;

    listen 80;
    listen 443;
    server_name "";
    return 444;
}

server {
#    access_log /var/log/nginx/flask_reddit/flask-reddit_access.log;
#    error_log /var/log/nginx/flask_reddit/flask-reddit_error.log;

    # 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 {
#    access_log /var/log/nginx/flask_reddit/flask-reddit_access.log;
#    error_log /var/log/nginx/flask_reddit/flask-reddit_error.log;

    # 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;
    }
}

/etc/nginx/nginx.conf(我的主要 Nginx 配置文件):

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/*;
}


#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;
#   }
#}

当我运行时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)
   Active: active (running) (Result: exit-code) since Thu 2019-08-29 04:07:42 UTC; 3 days ago
  Process: 21652 ExecReload=/usr/sbin/nginx -g daemon on; master_process on; -s reload (code=exited, status=0/SUCCESS)
 Main PID: 4855 (nginx)
    Tasks: 2
   Memory: 5.5M
      CPU: 1.521s
   CGroup: /system.slice/nginx.service
           ├─ 4855 nginx: master process /usr/sbin/nginx -g daemon on; master_process on
           └─21657 nginx: worker process                           

Sep 01 02:18:29 ip-172-26-5-151 systemd[1]: Reloading A high performance web server and a reverse proxy server.
Sep 01 02:18:29 ip-172-26-5-151 systemd[1]: Reloaded A high performance web server and a reverse proxy server.
Sep 01 04:58:21 ip-172-26-5-151 systemd[1]: Reloading A high performance web server and a reverse proxy server.
Sep 01 04:58:21 ip-172-26-5-151 systemd[1]: Reloaded A high performance web server and a reverse proxy server.
Warning: Journal has been rotated since unit was started. Log output is incomplete or unavailable.

我的sudo netstat -tulpn输出是:

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      4855/nginx -g daemo
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      4036/sshd       
tcp        0      0 0.0.0.0:25              0.0.0.0:*               LISTEN      19927/master    
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      4855/nginx -g daemo
tcp        0      0 127.0.0.1:8000          0.0.0.0:*               LISTEN      6398/python     
tcp        0      0 0.0.0.0:9001            0.0.0.0:*               LISTEN      20037/python    
tcp6       0      0 :::22                   :::*                    LISTEN      4036/sshd       
tcp6       0      0 :::25                   :::*                    LISTEN      19927/master    
udp        0      0 0.0.0.0:68              0.0.0.0:*                           943/dhclient    

使用sudo nginx -t表示这个主要的 Nginx 配置nginx.conf是有效的,但运行却sudo nginx -t -c /etc/nginx/sites-enabled/flask-reddit给出以下结果:

nginx: [emerg] "server" directive is not allowed here in /etc/nginx/sites-enabled/flask-reddit:1
nginx: configuration file /etc/nginx/sites-enabled/flask-reddit test failed

为什么会发生这种情况?

答案1

我将您的设置复制到一个盒子上并对其进行了调整,直到现在它可以正常工作...使用它作为您的位置,您会没事的

location / {
    # forward application requests to the gunicorn server
    proxy_pass http://localhost:8000;

    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';

    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-NginX-Proxy true;

}

相关内容