提供子目录的静态文件时出现Nginx 404问题

时间:2016-07-04 13:54:23

标签: nginx

尝试访问 sub 静态目录的CSS和JS文件时出现404错误。我尝试使用 = ,但仍然无效。

mydomain / sub / index.html 服务。

mydomain / sub / css / style.css 未提供

这是Nginx配置文件:

server {
  listen 80;
  server_name www.example.com example.com;

  return 301 https://www.example.com$request_uri;
}

server {
  # Global
  listen 443 ssl http2;
  server_name www.example.com example.com;
  charset utf-8;

  # Logs
  ...

  # SSL
  ...

  # OCSP Stapling
  ...

  # HSTS
  ....

  # Robots
  location /robots.txt {
    return 200 "User-agent: *\nAllow: /";
  }

  # Sub
  location /sub {
    alias /www/sub;
    index index.html;
    try_files $uri $uri/ =404;
  }

  # Page Speed
  include /etc/nginx/pagespeed.conf;

  # Set expiration policy
  location ~* \.(?:ico|css|js|gif|jpe?g|png|woff)$ {
    expires 7d;
    add_header Pragma public;
    add_header Cache-Control "public";

    proxy_pass http://example;
  }

  # Location
  location / {
    if ($request_method = POST) {
      return 500;
    }

    proxy_pass http://mydomain;
  }

}

1 个答案:

答案 0 :(得分:0)

在我的情况下,子目录应该使用子位置,希望对你有帮助。

    location /company/default/static/ {
        alias /data/pro/static_deployment/ ; 
        autoindex off; 
        location ~* \.(js|css|png|jpg|jpeg)$ {
            expires 7d; 
        }
    }