我正在尝试让nginx从根URL提供静态文件,例如。 http://localhost/favicon.ico
。我的静态资产位于/home/myuser/myapp/static/
中,而我的root /home/myuser/myapp/static/;
部分中有一个server
指令。
在我的浏览器中访问/favicon.ico
不会返回任何内容,但是在访问/static/favicon.ico
时可以正确提供文件。
这意味着nginx将/home/myuser/myapp/
当作/home/myuser/myapp/static/
的根,对吗?不应该将/favicon.ico
的请求从静态目录中移出吗?
这是我的nginx.conf:
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024; # increase if you have lots of clients
accept_mutex off; # set to 'on' if nginx worker_processes > 1
use epoll;
}
http {
include mime.types;
access_log /var/log/nginx/access.log combined;
sendfile on;
upstream app_server {
server unix:/sockets/gunicorn.sock fail_timeout=0;
}
server {
listen 80 deferred;
client_max_body_size 4G;
server_name 10.204.2.169 perf-test.debesys.net;
keepalive_timeout 5;
root /home/myuser/myapp/static;
location / {
try_files $uri @app;
}
location @app{
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app_server;
}
}
}
这是/home/myuser/myapp
的结构:
├── constants.py
├── __init__.py
├── main.py
├── setup.sh
├── static
│ └── favicon.ico
├── tasks.py
└── wsgi.py
答案 0 :(得分:0)
您应将根目录移到位置部分。
server {
listen 80 deferred;
client_max_body_size 4G;
server_name 10.204.2.169 perf-test.debesys.net;
keepalive_timeout 5;
location / {
try_files $uri @app;
root /home/myuser/myapp/static;
}