測(cè)試項(xiàng)目:react-demo
server { listen 8080; # server_name your.domain.com; root /home/root/react-demo/dist; index index.html index.htm; location / { try_files $uri $uri/ /index.html; } location ^~ /assets/ { gzip_static on; expires max; add_header Cache-Control public; } error_page 500 502 503 504 /500.html; client_max_body_size 20M; keepalive_timeout 10;}
執(zhí)行sudo service nginx restart重啟Nginx服務(wù),
訪問(wèn)項(xiàng)目,http://IP:8080/
注意事項(xiàng):
1、配置域名的話,需要80端口,成功后,只要訪問(wèn)域名即可訪問(wèn)的項(xiàng)目
2、如果你使用了React-Router的browserHistory 模式,請(qǐng)?jiān)贜ginx配置中加入如下配置:
location / { try_files $uri $uri/ /index.html;}
原理,因?yàn)槲覀兊捻?xiàng)目只有一個(gè)根入口,當(dāng)輸入類似/home的url時(shí),找不到這個(gè)頁(yè)面,這是,nginx會(huì)嘗試加載index.html,加載index.html之后,react-router就能起作用并匹配我們輸入的/home路由,從而顯示正確的home頁(yè)面,,如果browserHistory模式的項(xiàng)目沒(méi)有配置上述內(nèi)容,會(huì)出現(xiàn)404的情況。
可參考react-router文檔:
https://react-guide.github.io/react-router-cn/docs/guides/basics/Histories.html
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)錯(cuò)新站長(zhǎng)站的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
新聞熱點(diǎn)
疑難解答
圖片精選