【问题标题】:Nginx Static File Cache giving 404sNginx 静态文件缓存给出 404s
【发布时间】:2017-05-12 20:43:32
【问题描述】:

我尝试了很多不同的方法来使其正常工作,并查看了一堆以前的 Nginx 配置指南,但我的网站一直给我 404 未找到我所有的静态内容。下面是我的配置。

server {
    listen 80 default_server;
    listen [::]:80 default_server;
   server_name mywebsite.com www.mywebsite.com;
    return 301 https://$server_name$request_uri;
}
proxy_cache_path /tmp/nginx levels=1:2 keys_zone=my_zone:10m inactive=60m;
proxy_cache_key "$scheme$request_method$host$request_uri";

server {
        access_log off;
        listen 443 ssl http2 default_server;
        listen [::]:443 ssl http2 default_server;
        include snippets/ssl-mywebsite.com.conf;
        include snippets/ssl-params.conf;

        location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
                expires 30d;
                add_header Vary Accept-Encoding;
                access_log off;
        }

        # Pass requests for / to localhost:3000:
        location / {
                proxy_cache my_zone;
                proxy_cache_bypass  $http_cache_control;
                add_header X-Proxy-Cache $upstream_cache_status;
                include proxy_params;

                proxy_ignore_headers X-Accel-Expires Expires Cache-Control;
                proxy_ignore_headers Set-Cookie;
                proxy_hide_header Set-Cookie;
                proxy_hide_header X-powered-by;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-NginX-Proxy true;
                proxy_pass http://localhost:3000;
                proxy_ssl_session_reuse off;
                proxy_set_header Host $http_host;
                proxy_redirect off;
        }
}

关于更多背景,我的静态文件都存储在一个公共文件夹中,例如:/www/Project/public/css/styles.css 或 /www/Project/public/images/myimage.jpg

如果我删除该位置 ~* .(?:ico|css|js|gif ... ),该网站可以工作,但它不会缓存。

有人知道发生了什么吗?

【问题讨论】:

    标签: node.js nginx


    【解决方案1】:

    想通了。

    事实证明,我的根路径是错误的 - 我是相对于我的项目目录(我的 app.js 文件所在的位置)执行的,而它本应相对于我的文件系统上的绝对根目录。

    例如应该是 /home/mycomputerlogin/www/project/public/

    所以在我的 ~* 中。 第一行是 根 /home/mycomputerlogin/www/project/public; 之后效果很好。

    【讨论】:

      猜你喜欢
      • 2016-04-04
      • 2020-08-06
      • 2015-02-14
      • 2016-12-13
      • 2012-09-02
      • 2019-04-14
      • 2018-10-12
      • 2017-10-27
      • 2020-07-31
      相关资源
      最近更新 更多