关于react-router-dom的部署Apache&nginx配置解决方案

今天主要是整理BrowserRouter 去掉#之后 服务器的具体配置,主要讲述以下两种 Apache&nginx

  • Apache

    • 通过添加以下Rewrite *行来更改VirtualHost配置(通常位于/etc/httpd/conf.d/httpd.conf中)
    • 可以在此处找到常规Apache虚拟主机配置信息
<VirtualHost *:8080>
  ServerName example.com
  DocumentRoot /var/www/httpd/example.com
  <Directory "/var/www/httpd/example.com">
    RewriteEngine on
    #Do not rewrite files or directories
    #不要重写文件或目录
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^ - [L]
    # Rewrite everything else to index.html to allow html5 state links
    #将其他所有内容重写为index.html以允许html5状态链接
    RewriteRule ^ index.html [L]
  </Directory>
</VirtualHost>

#出处吴佳浩[https://www.jianshu.com/u/c817dc83befd](https://www.jianshu.com/u/c817dc83befd)

这告诉Apache提供任何存在的文件,但如果它们不存在,只需提供/ index.html而不是'404:not found`。

  • Nginx

通过提供以下位置来更改虚拟主机配置(通常位于/etc/nginx/conf.d/vhosts.conf中):

server {
    listen 80 default_server;
    server_name /var/www/example.com;

    root /var/www/example.com;
    index index.html index.htm;      

    location ~* \.(?:manifest|appcache|html?|xml|json)$ {
      expires -1;
      # access_log logs / static.log; #我通常不包含静态日志
      # access_log logs/static.log; # I don't usually include a static log
    }

    location ~* \.(?:css|js)$ {
      try_files $uri =404;
      expires 1y;
      access_log off;
      add_header Cache-Control "public";
    }
    #包含文件扩展名的任何路由(例如/devicesfile.js)
    # Any route containing a file extension (e.g. /devicesfile.js)
    location ~ ^.+\..+$ {
      try_files $uri =404;
    }

    # Any route that doesn't have a file extension (e.g. /devices)
    location / {
        try_files $uri $uri/ /index.html;
    }
}

转载请留言并著名出处,虽然我不知道为什么最近简shu总是莫名奇妙的封文章,但是能给大家提供一些帮助我很开心,也希望更多的前端小伙伴能够成长的更全面。
------------------------------------------------------------------------------------------------------------------吴佳浩201801

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容