使用 nginx 配置 Gunicorn - 当端口 8000 上 运行 时,nginx 显示 500 内部服务器错误

Configuring Gunicorn with nginx - nginx displaying 500 Internal Server Error when running on port 8000

我在本地计算机(而非服务器)上设置 nginx 和 Gunicorn 时遇到问题。我有一个 Django 项目,我希望能够通过 http://localhost:8000/ 访问它。在我的 nginx 起作用之前,我的 Gunicorn 服务器能够成功启动。但是,一旦我配置了我的 nginx 服务器,现在我的 Gunicorn 服务器将无法启动,因为 nginx 已经 运行 在我的本地主机上的端口 8000 上运行。当我访问 http://localhost:8000/ 时,我得到 500 - Internal Server Error。然而,当我访问 http://localhost:80/ 时,我得到了 Welcome to nginx! 网页。

为了配置 nginx 和 Gunicorn,我遵循了 this guide,跳过了第 5 步到第 7 步(包括)。我必须注意,在我的 /etc/nginxsites-availablesites-enabled 目录不存在,所以我必须创建它们。

我的 /etc/nginx/sites-available 目录中只有两个文件 - defaultmydjangoproject。两者具有相同的内容,并且在 /etc/nginx/sites-enabled 中都有它们的符号链接。它们的内容是:

server {
    listen 8000;
    server_name 127.0.0.1;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /production_static/ {
        root /home/john/Documents/Django_projects/mydjangoproject/Version_2/mydjangoproject;
    }

    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://127.0.0.1:8000;
    }
}

以下是我的 Django 项目 settings.py 文件的相关部分:

DEBUG = False

ALLOWED_HOSTS = ['*', '127.0.0.1', 'localhost']

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'production_static/')

MEDIA_ROOT = os.path.join(BASE_DIR,'media')
MEDIA_URL = '/media/'

STATICFILES_DIRS = [
    BASE_DIR / "static",
]

这是来自 /var/log/nginx/error.log 的错误日志:

2020/12/20 11:58:09 [emerg] 23584#23584: open() "/etc/nginx/proxy_params" failed (2: No such file or directory) in /etc/nginx/sites-enabled/mydjangoproject:11
2020/12/20 12:01:59 [emerg] 23773#23773: open() "/etc/nginx/proxy_params" failed (2: No such file or directory) in /etc/nginx/sites-enabled/mydjangoproject:11
2020/12/20 12:02:04 [emerg] 23775#23775: open() "/etc/nginx/proxy_params" failed (2: No such file or directory) in /etc/nginx/sites-enabled/mydjangoproject:11
2020/12/20 12:02:05 [emerg] 23777#23777: open() "/etc/nginx/proxy_params" failed (2: No such file or directory) in /etc/nginx/sites-enabled/mydjangoproject:11
2020/12/20 12:12:25 [error] 22879#22879: *1 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 127.0.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "127.0.0.1"
2020/12/20 13:51:08 [error] 22879#22879: *3 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 127.0.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "localhost"
2020/12/20 13:55:01 [error] 25938#25938: *1 "/etc/nginx/html/index.html" is not found (2: No such file or directory), client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "localhost:8000"
2020/12/20 13:55:01 [error] 25938#25938: *1 open() "/etc/nginx/html/favicon.ico" failed (2: No such file or directory), client: 127.0.0.1, server: , request: "GET /favicon.ico HTTP/1.1", host: "localhost:8000"
2020/12/20 13:55:04 [error] 25938#25938: *1 "/etc/nginx/html/index.html" is not found (2: No such file or directory), client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "localhost:8000"
2020/12/20 13:55:21 [error] 25938#25938: *1 "/etc/nginx/html/index.html" is not found (2: No such file or directory), client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "localhost:8000"
2020/12/20 13:55:21 [error] 25938#25938: *1 "/etc/nginx/html/index.html" is not found (2: No such file or directory), client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "localhost:8000"
2020/12/20 13:55:22 [error] 25938#25938: *1 "/etc/nginx/html/index.html" is not found (2: No such file or directory), client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "localhost:8000"
2020/12/20 13:55:23 [error] 25938#25938: *1 "/etc/nginx/html/index.html" is not found (2: No such file or directory), client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "localhost:8000"
2020/12/20 13:56:00 [error] 25938#25938: *1 "/etc/nginx/html/index.html" is not found (2: No such file or directory), client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "localhost:8000"
2020/12/20 13:56:48 [warn] 25999#25999: conflicting server name "127.0.0.1" on 0.0.0.0:8000, ignored
2020/12/20 13:57:11 [warn] 26011#26011: conflicting server name "127.0.0.1" on 0.0.0.0:8000, ignored
2020/12/20 13:57:14 [alert] 26013#26013: *1013 socket() failed (24: Too many open files) while connecting to upstream, client: 127.0.0.1, server: 127.0.0.1, request: "GET / HTTP/1.0", upstream: "http://127.0.0.1:8000/", host: "localhost:8000"
2020/12/20 13:57:15 [alert] 26013#26013: *2025 socket() failed (24: Too many open files) while connecting to upstream, client: 127.0.0.1, server: 127.0.0.1, request: "GET / HTTP/1.0", upstream: "http://127.0.0.1:8000/", host: "localhost:8000"
2020/12/20 13:57:16 [alert] 26013#26013: *3037 socket() failed (24: Too many open files) while connecting to upstream, client: 127.0.0.1, server: 127.0.0.1, request: "GET / HTTP/1.0", upstream: "http://127.0.0.1:8000/", host: "localhost:8000"
2020/12/20 13:57:31 [alert] 26013#26013: *4050 socket() failed (24: Too many open files) while connecting to upstream, client: 127.0.0.1, server: 127.0.0.1, request: "GET / HTTP/1.0", upstream: "http://127.0.0.1:8000/", host: "localhost:8000"
2020/12/20 14:01:02 [alert] 26013#26013: *5064 socket() failed (24: Too many open files) while connecting to upstream, client: 127.0.0.1, server: 127.0.0.1, request: "GET / HTTP/1.0", upstream: "http://127.0.0.1:8000/", host: "localhost:8000"
2020/12/20 14:01:04 [alert] 26013#26013: *6076 socket() failed (24: Too many open files) while connecting to upstream, client: 127.0.0.1, server: 127.0.0.1, request: "GET / HTTP/1.0", upstream: "http://127.0.0.1:8000/", host: "localhost:8000"

我用谷歌搜索了这些错误,但一无所获。 谁能告诉我为什么我的 nginx 显示 500 - Internal server error 以及我怎样才能让 Gunicorn 和 nginx 在相同的地址和端口上 运行(如果可能的话)?

如@iklinac 所述,您不能将两个端口配置为同一个 ID。端口应为 80,如 the tutorial you mentioned 中所示。不是原来的 8000 post。

server {
    listen 80;
    server_name server_domain_or_IP;
}