測試項目: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;}
執行sudo service nginx restart重啟Nginx服務,
訪問項目,http://IP:8080/
注意事項:
1、配置域名的話,需要80端口,成功后,只要訪問域名即可訪問的項目
2、如果你使用了React-Router的browserHistory 模式,請在Nginx配置中加入如下配置:
location / { try_files $uri $uri/ /index.html;}
原理,因為我們的項目只有一個根入口,當輸入類似/home的url時,找不到這個頁面,這是,nginx會嘗試加載index.html,加載index.html之后,react-router就能起作用并匹配我們輸入的/home路由,從而顯示正確的home頁面,,如果browserHistory模式的項目沒有配置上述內容,會出現404的情況。
可參考react-router文檔:
https://react-guide.github.io/react-router-cn/docs/guides/basics/Histories.html
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對VEVB武林網的支持。
新聞熱點
疑難解答