我已经按照 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;
}
}
Run Code Online (Sandbox Code Playgroud)
未提供静态文件(nginx 404)。如果我查看访问日志,似乎 nginx 正在查看 /etc/nginx/html/static... 而不是配置中指定的 /srv/static/ 。
我不知道为什么要这样做,任何帮助将不胜感激。
jol*_*ger 13
简短回答:请参阅长版中的最后一个位置。从技术上讲,该页面中描述的设置是错误的。原因见答案末尾。
长答案:您/etc/nginx...
的日志中可能有路径,因为对静态文件的请求与您的静态位置不匹配(正则表达式位置优先)并且您没有root
为server
块本身指定。
当您的请求与正则表达式位置匹配时,nginx 将搜索/etc/nginx
. 为了解决这个问题,你可能想root
直接在server
块内添加指令,或者在每个非代理位置下(同样适用于 fastCGI)。
还发现:如果您root
在 location 下指定指令,nginx 将搜索 下的文件$document_root/$location
,例如,/srv/static/static
其中 99% 不是您想要的。您可以使用alias
指令,但如果可能,nginx 文档更喜欢重新定义root
指令:
location /static {
root /srv;
}
Run Code Online (Sandbox Code Playgroud)
该alias
指令的工作方式与您预期的一样,因此您可能需要编写以下代码:
location /static {
alias /srv/static;
}
Run Code Online (Sandbox Code Playgroud)
至于正则表达式的位置,你可能想把它放在/static
location里面。此外,由于只有静态文件,您可以摆脱正则表达式位置,最终位置将如下所示:
location /static {
root /srv;
access_log off;
expires 30d;
}
Run Code Online (Sandbox Code Playgroud)
根据用户提供的命令,他希望大部分组件尽可能新鲜地运行在类似 Debian 的 Linux 上。但是,他并没有手动正确安装所有内容或仅使用 aptitude,而是开始从软件包中混合和安装软件。这将适用于某些开发盒,但这对任何生产都行不通。所以这里有几点:
virtualenv
.www-data:www-data
在 Debian 中运行。由于您的设置不打算在 Web 服务器上下文中运行任何代码,因此添加更多用户是无用的。/etc/nginx/nginx.conf
文件,这是完全错误的,尤其是在类似 Debian 的系统上。而不是这样做,您可能希望在其中创建sample_project
文件/etc/nginx/sites-available
并将其链接/etc/nginx/sites-enabled
到使 nginx 附加您的配置`:# cd /etc/nginx/sites-enabled && ln -s ../sites-available/sample_project sample_project
Run Code Online (Sandbox Code Playgroud)sample_project
在这种情况下,文件应该只包含server
块本身,仅此而已。log_format
可能还有一些其他指令)应该具有您的虚拟主机的上下文,因此应该放在文件中的server
块下sample_project
。您不会影响在此 Web 服务器上运行的其他服务的工作。您可以将log_format
指令放在文件中,但位于任何块之外和server
块本身之前。fastcgi_pass
指令include fastcgi_params
,不如编写并仅重新定义那些不适合当前设置的指令。*access.log
或*error.log
通配符。给这个最小的工作主机配置文件/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;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
15911 次 |
最近记录: |