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

Eternal_Ether

我在本地计算机(不是服务器)上设置 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,我遵循了这个指南,跳过了步骤 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 在相同的地址和端口上运行(如果可能的话)?

Coffee_Overflow

正如@iklinac 所提到的,您不能将两个端口配置为相同的 ID。端口应该是你提到的教程中的80 不是原帖中的 8000。

server {
    listen 80;
    server_name server_domain_or_IP;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Docker 编写 nginx 配置设置以在端口 8000 上广播

nginx 500内部服务器错误

在端口8000和443上将nginx配置为反向代理,以便在8000上运行的webapp。如何禁用与端口8000的连接?

如何为ubuntu服务器中的多个运行端口配置https nginx配置

Django,nginx,gunicorn:为什么在某些页面上出现服务器错误(500)?

使用 gunicorn 运行的 Flask + Gunicorn + NGINX 问题

MediaWiki Nginx 500内部服务器错误

无法使用 nginx 在快速服务器上配置 SSL

使用 django 和 nginx 将数据库从 sqlite 更改为 postgres 时出现 500 内部服务器错误

在Pythonanywhere上配置Nginx服务器

Nginx + Gunicorn + Flask-> X个小时使用后持续出现500个错误

在 nginx 502 错误网关错误上使用 gunicorn 烧瓶

使用Python 3.4将Django / Gunicorn部署到Nginx服务器发现模板的路径错误

使用gunicorn和nginx运行的django:400错误的请求

带有Gunicorn的Flask App在POST上的Nginx服务器错误

将Nginx端口转发到Gunicorn实例

nginx + gunicorn 400错误

用于在端口3000上运行的Express App的Nginx配置

使用 nginx 在服务器上部署 laravel 出现 500 错误

运行时,罐子配置服务器端口

在 Ubuntu 14.04 可信服务器上使用 nginx 和 gunicorn 的多个 Django 应用程序

如何解决未知的内部服务器错误(500)Nginx给我?

带有 nginx 的 Droplet 和谷歌云存储之间的状态 500 内部服务器错误

在IP和端口下而不是域名下托管服务器的Nginx配置

如何在Nginx配置的上游块中公开服务器端口?

Jenkins 与 nginx 一起使用 docker 端口 50000 配置

在 AWS EC2 上运行 Flask 服务器,只有 gunicorn 但没有 nginx

端口80的Nginx错误

使用 Nginx 和 Gunicorn 在同一台服务器上的多个 Mezzanine 项目:“找不到服务器 IP 地址”