zoukankan      html  css  js  c++  java
  • Nginx配置---解决History路由报错问题

    目录

    1. 功能描述
    2. 代码实现

    一、功能描述

    • 实现
      • vue项目中使用history模式的路由时,解决访问深层级的路由返回404的问题。
      • nginx配置了ssl证书的情况下,解决history路由刷新报404的问题。
    • 版本:nginx/1.16.1。

    二、代码实现

    nginx.conf中相关配置:

        server {
            listen       80;
            server_name  <your-server-name>;
    
            # http 转成 https,配置了ssl证书时启用
            return 301 https://$host$request_uri;
    
            # 解决history路由刷新问题
            location / {
                # index.html文件在服务器中的存储目录
                root /data/www;  # /data/www需要修改为你服务器中的目录
                index index.html index.htm;
                
                #资源访问失败后定向到index.html
                try_files $uri $uri/ /index.html;
            }
    
            error_page 404 /404.html;
            location = /404.html {
            }
    
            error_page 500 502 503 504 /50x.html;
            location = /50x.html {
            }
        }
        
        # SSL证书配置
        server {
            listen       443 ssl;
            server_name  <your-server-name>;
    
            ssl_certificate <your_ssl_certificate_filepath>;
            ssl_certificate_key <your_ssl_certificate_key_filepath>;
            ssl_session_cache shared:SSL:1m;
            ssl_session_timeout  5m;
            ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
            ssl_prefer_server_ciphers on;
    
            # 解决http转https后路由报错问题
            location / {
                root /data/www;
                index index.html index.htm;
                try_files $uri $uri/ /index.html;
            }
    
            error_page 404 /404.html;
            location = /404.html {
            }
    
            error_page 500 502 503 504 /50x.html;
            location = /50x.html {
            }
        }
    

    三、最终效果

    demo地址

  • 相关阅读:
    BEGINNING SHAREPOINT&#174; 2013 DEVELOPMENT 第11章节--为Office和SP解决方式开发集成Apps 集成SP和Office App
    jQuery 处理TextArea
    Raphael的拖动处理
    CSS的position设置
    SVG的内部事件添加
    SVG的a链接
    SVG的text使用
    SVG的path的使用
    SVG的Transform使用
    Java中两个List对比的算法
  • 原文地址:https://www.cnblogs.com/snaillu/p/14123388.html
Copyright © 2011-2022 走看看