美文网首页
Nginx部署二级目录项目,访问静态资源404问题解决方案

Nginx部署二级目录项目,访问静态资源404问题解决方案

作者: l2en | 来源:发表于2019-07-11 21:39 被阅读0次

    场景


    项目需要区分系统使用者身份,存在诸如xxx/admin 、 xxx/client这样的访问地址。 因此在需要进行对相应项目进行独立部署

    部署环境


      os: mac(10.12.6)
      服务器: Nginx (1.17.1)
      前端项目开发框架: Umi (2.8.11)
    

    解决方案一:(nginx指定静态资源路径)

    项目配置


    Umi中config配置(config/config.js)添加如下参数
    base: '/admin'   (如果为client端,相应为base: '/client');
    

    参考;https://umijs.org/zh/guide/deploy.html#%E9%83%A8%E7%BD%B2-html-%E5%88%B0%E9%9D%9E%E6%A0%B9%E7%9B%AE%E5%BD%95
    原理请查阅react-router的basename

    Nginx配置


    Nginx (www)文件结构如下
        ├── 50x.html
        ├──admin
        │ ├──index.html
        │ ├──latout.css
        │ ├──main.js
        ├──client
        │ ├──index.html
        │ ├──latout.css
        │ ├──main.js
    
    Nginx配置如下
      
    #user  nobody;
    worker_processes  1;
    
    #error_log  logs/error.log;
    #error_log  logs/error.log  notice;
    #error_log  logs/error.log  info;
    
    #pid        logs/nginx.pid;
    
    
    events {
        worker_connections  1024;
    }
    
    
    http {
        include       mime.types;
        default_type  application/octet-stream;
    
        #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
        #                  '$status $body_bytes_sent "$http_referer" '
        #                  '"$http_user_agent" "$http_x_forwarded_for"';
    
        #access_log  logs/access.log  main;
    
        sendfile        on;
        #tcp_nopush     on;
    
        #keepalive_timeout  0;
        keepalive_timeout  65;
    
        #gzip  on;
        server {
            listen      9898;
            server_name  localhost;
    
            #charset koi8-r;
    
            #access_log  logs/host.access.log  main;
            # proxy_set_header Host $host:$server_port; 
          
            # 重点:解决二级目录部署时,访问css、js等静态资源报404的问题
            location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|css|js|eot|svg|ttf|woff|woff2|properties|json)$ {
            #   root  /usr/local/var/www/admin;
              if (-f $request_filename) {
                expires 1d;
                break;
                }
            }
    
            location /admin {
                alias   /usr/local/var/www/admin;
                # 重点:解决刷新后404问题 
                try_files $uri /admin/index.html;
                index  index.html index.htm;
            }
            location /client {
                alias   /usr/local/var/www/client;
                # 重点:解决刷新后404问题
                try_files $uri /client/index.html;  
                index  index.html index.htm;
            }
    
            location /api {
              proxy_pass http://192.168.110.158:3001/api/v1;
            }
    
            #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   /usr/local/var/www/50x.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;
            #}
        }
    
    
        # another virtual host using mix of IP-, name-, and port-based configuration
        #
        #server {
        #    listen       8000;
        #    listen       somename:8080;
        #    server_name  somename  alias  another.alias;
    
        #    location / {
        #        root   html;
        #        index  index.html index.htm;
        #    }
        #}
    
    
        # HTTPS server
        #
        #server {
        #    listen       443 ssl;
        #    server_name  localhost;
    
        #    ssl_certificate      cert.pem;
        #    ssl_certificate_key  cert.key;
    
        #    ssl_session_cache    shared:SSL:1m;
        #    ssl_session_timeout  5m;
    
        #    ssl_ciphers  HIGH:!aNULL:!MD5;
        #    ssl_prefer_server_ciphers  on;
    
        #    location / {
        #        root   html;
        #        index  index.html index.htm;
        #    }
        #}
        include servers/*;
    }
    
    

    解决方案二(webpack配置publicPath)

    1、 项目配置

    base: '/admin/',
    publicPath: '/admin/',
    

    2、移除Nginx中静态资源配置项: location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|css|js|eot|svg|ttf|woff|woff2|properties|json)$ {...}

    解释:

    base做为路由路径,为匹配nginx的 location ^~ /admin{ }
    publicPath为静态资源指定路径,打包出来css与js引入路径如图所示:


    css.jpg
    js.jpg

    su.

    参考本文可解决:SPA应用部署后刷新页面404问题,css/js/png等静态文件无法访问,或访问404问题
    参看本文原则上可解决Nginx多层级目录部署问题;

    相关文章

      网友评论

          本文标题:Nginx部署二级目录项目,访问静态资源404问题解决方案

          本文链接:https://www.haomeiwen.com/subject/beyjkctx.html