nginx配置静态验证文件,报404,解决方案

wylc123 1年前 ⋅ 1830 阅读

1. 问题背景

       自己springboot开发的博客系统,jar包发布,类似百度收录地图文件site.txt,死链提交文件silian.xml不好部署,考虑使用nginx直接静态部署,部署后发现访问报404

 

2. 问题出现原因

2.1 nginx静态资源配置
#若用alias的话,则访问127.0.0.1/img/目录里面的文件时,ningx会自去/var/www/image/目录找文件
location /img/ {
    alias /var/www/image/;
}

#若用root的话,则访问/127.0.0.1/img/目录下的文件时,nginx会自动去/var/www/image/img/目录下找文件
location /img/ {
    root /var/www/image;
}
2.2 没有在https配置里添加静态资源代理配置

如果设置了ssl,https配置的话,需要在相关配置里也加上静态资源的访问配置。

3. 最终配置

#个人原创博客
    server {
                listen       80;
                server_name  www.daxueyiwu.com;
                rewrite ^(.*)$ https://$host$1 permanent;   #将所有http请求通过rewrite重定向到https。
                #charset koi8-r;

                #access_log  logs/host.access.log  main;
                location / {
                        client_max_body_size 200m; #文件上传大小
                        root   html;
                        index  index.html index.htm;
                        proxy_pass http://localhost:8080/;
                }
                location /site/ {
                    alias   /usr/softs/apps/site/;
                }


#               location /site {
                  #  root /usr/softs/apps/site;
                   # #默认请求转到root路径下的index.html页面。
                 #   index index.html;

                #}

                #error_page  404              /404.html;

                # redirect server error pages to the static page /50x.html
                #
                error_page   500 502 503 504  /50x.html;
                location = /50x.html {
                        root   html;
                }

                # proxy the PHP scripts to Apache listening on 127.0.0.1:80
                #
                #location ~ \.php$ {
                #    proxy_pass   http://127.0.0.1;
                #}

                # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
                #
                #location ~ \.php$              #    root           html;
                #    fastcgi_pass   127.0.0.1:9000;
                #    fastcgi_index  index.php;
                #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
                #    include        fastcgi_params;
                #}

                # deny access to .htaccess files, if Apache's document root
                # concurs with nginx's one
                #
                #location ~ /\.ht {
                #    deny  all;
                #}
    }
        #www.daxueyiwu.com https配置
        server {
        listen       443 ssl;  #SSL协议访问端口号为443。此处如未添加ssl,可能会造成Nginx无法启动。
        server_name  www.daxueyiwu.com;  #将localhost修改为您证书绑定的域名,例如:www.example.com。
        ssl_certificate      cert/3498037_www.daxueyiwu.com.pem; #将domain name.pem替换成您证书的文件名。
        ssl_certificate_key  cert/3498037_www.daxueyiwu.com.key; #将domain name.key替换成您证书的密钥文件名。

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

        ssl_ciphers   ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;  #使用此加密套件。
                ssl_protocols TLSv1 TLSv1.1 TLSv1.2;   #使用该协议进行配置。
        ssl_prefer_server_ciphers  on;
                if ($scheme = http) {
                        return 301 https://$host$request_uri;
                }
        location / {
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                        proxy_set_header Host $http_host;
                        proxy_set_header X-Forwarded-Proto https;
                        proxy_redirect off;
                        proxy_connect_timeout      240;
                        proxy_send_timeout         240;
                        proxy_read_timeout         240;
                        # note, there is not SSL here! plain HTTP is used
                        proxy_pass http://localhost:8080;
                        proxy_redirect http:// https://;
                        #proxy_set_header Upgrade $http_upgrade;
                        #proxy_set_header Connection "upgrade";
        }
#这个地方也要做配置,来跳转https地址
        location /site/ {
                    alias   /usr/softs/apps/site/;
                }
    }

 

 

更多内容请访问:IT源点

相关文章推荐

全部评论: 0

    我有话说: