react router項目部署nginx 配置問題

背景:項目中使用了react、react-router開發(fā),在部署到nginx服務(wù)器時遇到了以下問題

history

history url樣例 特點
hash history /#/user/profile 不需要服務(wù)器支持
browser history /user/profile react-router官方推薦,需要服務(wù)器支持(因為是SPA項目,url切換時需要服務(wù)器始終返回index.html)

nginx配置

如下介紹使用browser history模式部署到nginx服務(wù)器

部署到nginx根目錄

訪問路徑:http://localhost/

# nginx配置
location / {
    root   html;
    index  index.html;
    # url 切換時始終返回index.html
    try_files $uri /index.html;
}

部署到nginx子目錄

假設(shè)部署到/app目錄下,訪問路徑:http://localhost/app

# nginx配置
location /app {
    root   html;
    index  index.html;
    # url 切換時始終返回index.html
    try_files $uri /app/index.html;
}
# 圖片樣式緩存1年
location ~* /app.*\.(js|css|png|jpg)$
{
    access_log off;
    expires    365d;
}
# html/xml/json 文件不緩存
location ~* /app.*\.(?:manifest|appcache|html?|xml|json)$
{
    expires    -1;
}
// package.json
"homepage": "http://localhost/app",
// react-router路由配置
// 注意指定basename
<BrowserRouter basename='/app'>
</BrowserRouter>

開啟nginx的gzip壓縮

# 開啟gzip
gzip on;

# 啟用gzip壓縮的最小文件,小于設(shè)置值的文件將不會壓縮
gzip_min_length 1k;

# gzip 壓縮級別,1-10,數(shù)字越大壓縮的越好,也越占用CPU時間
gzip_comp_level 1;

# 進行壓縮的文件類型。javascript有多種形式。其中的值可以在 mime.types 文件中找到。
gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;

# 是否在http header中添加Vary: Accept-Encoding,建議開啟
gzip_vary on;

# 禁用IE 6 gzip
gzip_disable "MSIE [1-6]\.";

整體配置

# nginx.conf整體配置大概如下:
http {
    # 開啟gzip
    gzip on;
    # 啟用gzip壓縮的最小文件,小于設(shè)置值的文件將不會壓縮
    gzip_min_length 1k;
    # gzip 壓縮級別,1-10,數(shù)字越大壓縮的越好,也越占用CPU時間,后面會有詳細說明
    gzip_comp_level 1;
    # 進行壓縮的文件類型。javascript有多種形式。其中的值可以在 mime.types 文件中找到。
    gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php;
    # 是否在http header中添加Vary: Accept-Encoding,建議開啟
    gzip_vary on;
    # 禁用IE 6 gzip
    gzip_disable "MSIE [1-6]\.";

    server {
        location /app {
            root   html;
            index  index.html;
            # url 切換時始終返回index.html
            try_files $uri /app/index.html;
        }
        # 圖片樣式緩存1年
        location ~* /app.*\.(js|css|png|jpg)$
        {
            access_log off;
            expires    365d;
        }
        # html/xml/json 文件不緩存
        location ~* /app.*\.(?:manifest|appcache|html?|xml|json)$
        {
            expires    -1;
        }
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,715評論 19 139
  • Page 1:nginx 服務(wù)器安裝及配置文件詳解 CentOS 6.2 x86_64 安裝 nginx 1.1 ...
    xiaojianxu閱讀 8,696評論 1 41
  • 第一章 Nginx簡介 Nginx是什么 沒有聽過Nginx?那么一定聽過它的“同行”Apache吧!Ngi...
    JokerW閱讀 33,042評論 24 1,002
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,366評論 25 708
  • 小河北岸 老木屋前 信箱子里 一封素箋 淚已濕了半張 他回憶起 曾愛的模樣 模糊了,清晰了,消失了... 再也看不...
    曾有個男孩閱讀 256評論 0 3

友情鏈接更多精彩內(nèi)容