我已经遵循了从django wiki( https://code.djangoproject.com/wiki/DjangoAndNginx )用nginx设置django的指令,并按如下所示进行了nginx设置(几个名称更改以适应我的设置)。
user nginx nginx; worker_processes 2; error_log /var/log/nginx/error_log info; events { worker_connections 1024; use epoll; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] ' '"$request" $status $bytes_sent ' '"$http_referer" "$http_user_agent" ' '"$gzip_ratio"'; client_header_timeout 10m; client_body_timeout 10m; send_timeout 10m; connection_pool_size 256; client_header_buffer_size 1k; large_client_header_buffers 4 2k; request_pool_size 4k; gzip on; gzip_min_length 1100; gzip_buffers 4 8k; gzip_types text/plain; output_buffers 1 32k; postpone_output 1460; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 75 20; ignore_invalid_headers on; index index.html; server { listen 80; server_name localhost; location /static/ { root /srv/static/; } location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|mov) { access_log off; expires 30d; } location / { # host and port to fastcgi server fastcgi_pass 127.0.0.1:8080; fastcgi_param PATH_INFO $fastcgi_script_name; fastcgi_param REQUEST_METHOD $request_method; fastcgi_param QUERY_STRING $query_string; fastcgi_param CONTENT_TYPE $content_type; fastcgi_param CONTENT_LENGTH $content_length; fastcgi_pass_header Authorization; fastcgi_intercept_errors off; fastcgi_param REMOTE_ADDR $remote_addr; } access_log /var/log/nginx/localhost.access_log main; error_log /var/log/nginx/localhost.error_log; } }
静态文件没有被提供(nginx 404)。 如果我查看访问日志,看起来nginx正在查看/ etc / nginx / html / static …而不是/ srv / static /,如configuration中指定的那样。
我不知道为什么这样做,任何帮助将非常感激。
简短的回答 :查看长版本中的最后一个位置。 从技术上讲,该页面中描述的设置是错误的。 出于某些原因查看答案的结尾。
长的回答 :你在/etc/nginx...path中存在/etc/nginx...path,因为你的静态文件的请求不符合你的静态位置(正则expression式的位置是先例 ),你没有为server块指定root 。
当你的请求匹配正则expression式的位置时,nginx会search/etc/nginx下的文件。 为了解决这个问题,你可能想直接在server块内部添加root指令,如上所述,或者在每个非代理位置下(和fastCGI一样)。
还发现:如果你指定位置下的root指令nginx将search$document_root/$location下的$document_root/$location ,例如在/srv/static/static这是99%,不是你可能想要的。 你可以使用alias指令,但如果可能的话, nginx文档更喜欢重新定义root指令:
location /static { root /srv; }
alias指令的工作方式与您预期的相同,因此您可能需要这样写:
location /static { alias /srv/static; }
至于正则expression式的位置,你可能想把它放在/static位置。 也因为只有静态文件,你可以摆脱它的正则expression式的位置,最终的位置将如下所示:
location /static { root /srv; access_log off; expires 30d; }
根据用户提供的命令,他喜欢将大部分组件尽可能地运行在类似Debian的Linux上。 但不是正确安装所有东西或者只是使用aptitude,他也开始混合和安装软件包。 这将适用于某些开发箱,但是这对于任何生产都是不可行的。 所以这里有几点:
virtualenv 。 www-data:www-data在Debian中运行。 由于您的设置不打算在Web服务器上下文中运行任何代码,添加更多的用户是没有用的。 /etc/nginx/nginx.conf文件,特别是在类似Debian的系统上。 而不是这样做,你可能想在/etc/nginx/sites-available创buildsample_project文件,并将其链接到/etc/nginx/sites-enabled使nginx附加你的configuration:
# cd /etc/nginx/sites-enabled && ln -s ../sites-available/sample_project sample_project
sample_project文件应该只包含server块本身,仅此而已。 log_format和其他一些指令)都应该有你的虚拟主机的上下文,因此应该放在你的sample_project文件的server块下。 您不会影响在此Web服务器上运行的其他服务的工作。 您可以将log_format指令放置在您的文件中,但不在任何块中,并在server块本身之前。 fastcgi_pass指令,而应写入include fastcgi_params而只重新定义那些不正确的指令。 *access.log或*error.log通配符。 为此,最小工作主机configuration文件/etc/nginx/sites-available/sample_project可能如下所示:
server { listen 80; server_name myhostname.com; root /home/user/django/sample_project; location /static { root /srv; access_log off; expires 30d; } location / { include fastcgi_params; fastcgi_pass 127.0.0.1:8080; } access_log /var/log/nginx/sample_project.access.log combined; error_log /var/log/nginx/sample_project.error.log warn; }
您在服务器块中使用server_name localhost 。 这意味着nginx将只响应具有http主机头字段'localhost'或可能是127.0.0.1的请求。
具有其他http主机字段的请求将成为nginx的默认位置,这是 – 我以为 – /usr/share/nginx/html 。
如果您想确保您的服务器块接受所有传入请求,请使用server_name "" 。 虽然这可能不是你想要的djangoconfiguration。