在http-> https之后,nginx找不到静态内容

时间:2018-08-26 07:10:38

标签: ssl nginx https http-status-code-404

我为使用http的nginx提供静态文件服务进行了有效的设置。

后来,当我切换到https时,将端口从80更改为443,并添加了ssl证书和密钥,因此Web服务器无法再找到静态文件。我收到所有静态文件的404响应。这是我的nginx配置文件:

http {

    include /etc/nginx/mime.types;
    default_type application/octet-stream;
    sendfile on;
    keepalive_timeout 65;


    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; 
    ssl_prefer_server_ciphers on;


    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    upstream http_backend {
            server 127.0.0.1:3001;
            keepalive 32;
    }

    server {
            listen       80;
            server_name  test.com;

            # Redirect to https
            location / {
                    rewrite ^ https://$host$request_uri permanent;
            }
    }

    server {
            listen 443 default_server ssl;
            server_name localhost;
            root /var/www/myblog/app/resources/public;

            ssl on;
            ssl_certificate /etc/nginx/certificate.crt;
            ssl_certificate_key /etc/nginx/key.pem;

            location / {
                    proxy_redirect off;

                    proxy_pass              http://http_backend;
                    proxy_set_header        Host $host;
                    proxy_set_header        X-Real-IP $remote_addr;
                    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
                    proxy_set_header        X-Forwarded-Host $server_name;
                    proxy_set_header        X-Forwarded-Proto https;

                    access_log /var/www/logs/myblog.access.log;
                    error_log /var/www/logs/myblog.error.log;
            }

            location ^~ /(css|js) {
                    root /var/www/myblog/app/resources/public;
            }
    }
}

我的访问日志:

37.201.226.254 - - [26/Aug/2018:06:41:25 +0000] "GET /js/compiled/foo.js HTTP/1.1" 404 38 "https://www.test.com/" "Mozilla/5.0 (X11; Linux x86_64; rv:61.0) Gecko/20100101 Firefox/61.0"    
37.201.226.254 - - [26/Aug/2018:06:41:25 +0000] "GET /css/mui.css HTTP/1.1" 404 38 "https://www.test.com/" "Mozilla/5.0 (X11; Linux x86_64; rv:61.0) Gecko/20100101 Firefox/61.0"           
37.201.226.254 - - [26/Aug/2018:06:41:25 +0000] "GET /css/style.css HTTP/1.1" 304 0 "https://www.test.com/" "Mozilla/5.0 (X11; Linux x86_64; rv:61.0) Gecko/20100101 Firefox/61.0"

文件位于/var/www/myblog/app/resources/public中:已配置为root。我从/var/www/myblog/app运行Web服务器。

请记住,这是我第一次使用NGINX进行部署。有谁知道我忘记了配置为使用https提供静态文件的想法?我的操作系统是linux。

1 个答案:

答案 0 :(得分:0)

我通过随机试验和错误找出了问题所在。由于某些原因,更改

result = ds.select(Col a)

        location ^~ /(css|js) {
                root /var/www/myblog/app/resources/public;
        }

似乎可以正常工作。