我在SO上看到了这个问题的许多很好的答案,但要么我不理解它们,要么它们不适用于我的特定情况。我在这里看过:Serving static files with Nginx + Gunicorn + Django和许多其他的。我已经遵循了这些答案中的建议,但我仍然没有有效的解决方案。我希望如果我准确地解释了我在做什么,那么也许有人会告诉我我哪里错了。
我和其他几个团队在一个共享的环境中开发,我们共享一个ngnix服务器。我在这个共享服务器sre-dev.example.com上有一个Django项目。Django项目的路径是/apps/capman/capman_port10001/capman。
在我的settings.py中,我设置了以下值:
STATIC_ROOT = '/apps/capman/capman_port10001/capman/static'
STATIC_URL = '/static/'
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'corsheaders',
'rest_framework',
'rest_framework.authtoken',
...
'django_db_logger',
'rest_framework_swagger'
]我已经创建了一个/etc/nginx/sites-enabled目录和sym链接,
ln -s /apps/capman/capman_port10001/capman/nginx.conf caasa-dev.example.com我还在sre-dev的DNS中创建了别名(CNAME)和caasa-dev.example.com。在中添加了一个包含以下内容的nginx.conf文件/apps/capman/capman_port10001/capman/nginx.conf:
server {
listen 10001;
server_name caasa-dev.example.com;
location /static {
alias /apps/capman/capman_port10001/capman/static/;
}
}我执行了..。
python manage.py collectstatic..。几次之后,文件就在那里了:
$ pwd
/apps/capman/capman_port10001/capman/static
$ ls
admin rest_framework rest_framework_swagger我已经重新启动了nginx和gunicron。我的gunicorn服务器有一个systemd配置,因此我可以执行以下操作:
systemctl restart gnuicorn-capman.service如果我执行systemctl -l status gnuicorn-capman.service,我会得到以下输出:
# systemctl -l status gnuicorn-capman.service
● gnuicorn-capman.service - capman gnuicron service
Loaded: loaded (/etc/systemd/system/gnuicorn-capman.service; enabled; vendor preset: disabled)
Active: active (running) since Fri 2018-12-14 12:42:40 GMT; 17min ago
Process: 14949 ExecStopPost=/bin/rm -rf /run/gnuicorn-capman (code=exited, status=0/SUCCESS)
Process: 14946 ExecStop=/bin/kill -s TERM $MAINPID (code=exited, status=0/SUCCESS)
Process: 14955 ExecStartPre=/bin/chown -R sreuser:example /run/gnuicorn-capman /apps/capman/capman_port10001 (code=exited, status=0/SUCCESS)
Process: 14953 ExecStartPre=/bin/mkdir /run/gnuicorn-capman (code=exited, status=0/SUCCESS)
Main PID: 14960 (gunicorn)
CGroup: /system.slice/gnuicorn-capman.service
├─14960 /apps/capman/capman_port10001/env/bin/python2.7 /apps/capman/capman_port10001/env/bin/gunicorn --workers 4 capman.wsgi --bind 0.0.0.0:10001 --access-logfile ./access.log --error-logfile ./error.log --timeout 600
├─14966 /apps/capman/capman_port10001/env/bin/python2.7 /apps/capman/capman_port10001/env/bin/gunicorn --workers 4 capman.wsgi --bind 0.0.0.0:10001 --access-logfile ./access.log --error-logfile ./error.log --timeout 600
├─14968 /apps/capman/capman_port10001/env/bin/python2.7 /apps/capman/capman_port10001/env/bin/gunicorn --workers 4 capman.wsgi --bind 0.0.0.0:10001 --access-logfile ./access.log --error-logfile ./error.log --timeout 600
├─14970 /apps/capman/capman_port10001/env/bin/python2.7 /apps/capman/capman_port10001/env/bin/gunicorn --workers 4 capman.wsgi --bind 0.0.0.0:10001 --access-logfile ./access.log --error-logfile ./error.log --timeout 600
└─14971 /apps/capman/capman_port10001/env/bin/python2.7 /apps/capman/capman_port10001/env/bin/gunicorn --workers 4 capman.wsgi --bind 0.0.0.0:10001 --access-logfile ./access.log --error-logfile ./error.log --timeout 600然而,当我尝试访问caasa-dev.example.com:10001/admin/login/?next=/admin/时,仍然得不到静态内容:

我也会收到这样的消息...
... "GET /static/admin/css/base.css HTTP/1.1" 404 2793 "http://caasa-dev.example.com:10001/admin/login/?next=/admin/" "Mozilla/5.0 (Macintosh; Intel Mac OS X..。在我的access.log里。
那么我遗漏了什么呢?我做错了什么?
更新:如果我去caasa-dev.example.com:10001/static/,我会看到标准的Django404错误页面。
更新2:
由于某些原因,我的日志文件是空的:
[root@sre-dev sites-enabled]# systemctl -l status nginx
● nginx.service - The NGINX HTTP and reverse proxy server
Loaded: loaded (/etc/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since Fri 2018-12-14 12:42:37 GMT; 56min ago
Process: 14926 ExecStop=/bin/kill -s QUIT $MAINPID (code=exited, status=0/SUCCESS)
Process: 14932 ExecStart=/apps/nginx/sbin/nginx (code=exited, status=0/SUCCESS)
Process: 14930 ExecStartPre=/apps/nginx/sbin/nginx -t (code=exited, status=0/SUCCESS)
Main PID: 14934 (nginx)
CGroup: /system.slice/nginx.service
├─14934 nginx: master process /apps/nginx/sbin/ngin
└─14935 nginx: worker process
Dec 14 12:42:37 sre-dev systemd[1]: Starting The NGINX HTTP and reverse proxy server...
Dec 14 12:42:37 sre-dev nginx[14930]: nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
Dec 14 12:42:37 sre-dev nginx[14930]: nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
Dec 14 12:42:37 sre-dev systemd[1]: Started The NGINX HTTP and reverse proxy server.
[root@sre-dev sites-enabled]# ps -ef | grep nginx
root 14934 1 0 12:42 ? 00:00:00 nginx: master process /apps/nginx/sbin/nginx
nobody 14935 14934 0 12:42 ? 00:00:00 nginx: worker process
root 26500 3799 0 13:40 pts/8 00:00:00 grep --color=auto nginx
[root@sre-dev sites-enabled]# vim /etc/nginx/nginx.conf
[root@sre-dev sites-enabled]# grep log /etc/nginx/nginx.conf
error_log /var/log/nginx/error.log;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
access_log /var/log/nginx/access.log main;
[root@sre-dev sites-enabled]# ls -l /var/log/nginx/error.log
-rw-r--r--. 1 nginx root 0 Jun 1 2018 /var/log/nginx/error.log
[root@sre-dev sites-enabled]# ls -l /var/log/nginx/access.log
-rw-r--r--. 1 nginx nginx 0 Jun 7 2018 /var/log/nginx/access.log更新3:
谢谢伊凡,但是我的/apps/capman/capman_port10001/capman/nginx.conf应该是:
server {
listen 80;
server_name caasa-dev.example.com;
location /static {
alias /apps/capman/capman_port10001/capman/static/;
}
location / {
proxy_pass http://127.0.0.1:10001;
}
}或
server {
listen 10001;
server_name caasa-dev.example.com;
location /static {
alias /apps/capman/capman_port10001/capman/static/;
}
location / {
proxy_pass http://127.0.0.1:80;
}
}更新4:
我刚刚注意到我的/etc/nginx/nginx.conf没有包含我的站点启用的目录:
[root@sre-dev nginx]# grep -r include *
nginx.conf:include /usr/share/nginx/modules/*.conf;
nginx.conf: include /etc/nginx/mime.types;
nginx.conf: # See http://nginx.org/en/docs/ngx_core_module.html#include
nginx.conf: include /etc/nginx/conf.d/*.conf;
nginx.conf: include /etc/nginx/default.d/*.conf;
nginx.conf:# include /etc/nginx/default.d/*.conf;
nginx.conf.default: include mime.types;
nginx.conf.default: # include fastcgi_params;我是不是应该加上这行...
include /etc/nginx/sites-enabled/*.conf..。添加到我的/etc/nginx/nginx.conf中,如果有,在该文件中的什么位置?
发布于 2018-12-14 21:44:47
gunicorn --workers 4 capman.wsgi --bind 0.0.0.0:10001
nginx:listen 10001;
我猜nginx甚至没有启动(参见等同端口?)。所有请求都由Django处理。更改端口。
如果这是您拥有的完整的Nginx配置,那么它就缺少将请求转发到django的proxy_pass block
发布于 2018-12-14 23:12:09
问题是你正在使用gunicorn为django提供服务,但是nginx没有监听这个端口,所以反向代理与你的域名相连。使用此nginx配置:
server {
listen 80;
server_name caasa-dev.example.com;
location /static {
alias /apps/capman/capman_port10001/capman/static;
}
location / {
proxy_pass http://127.0.0.1:10001;
}
}在此配置中,nginx充当反向代理,并将所有流量路由到您的域的端口80,到由gunicorn处理的内部网络127.0.0.1:10001。
请注意,您正在使用 on address 0.0.0.0。我不建议你这么做。将其改为127.0.0.1,或者使用linux .sock文件以获得更好的配置。
https://stackoverflow.com/questions/53780499
复制相似问题