为什么Nginx在错误的目录中提供静态文件?

时间:2019-12-20 17:00:36

标签: nginx

我正在尝试让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

1 个答案:

答案 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;
    }