AngularJS / Vue / React 前端站點若是使用了單頁面模式,在開發模式下直接使用 url 路由訪問是正常的,可是在 nginx 下面會直接報 404 錯誤。html
解決方式是加入 try_files $uri $uri/ /index.html; 判斷若是文件不存在則重定向到 /index.html 頁面前端
worker_processes auto; #daemon off; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { use epoll; worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; server { listen 80; server_name 127.0.0.1; location / { root /etc/nginx/html/MY00_COM; try_files $uri $uri/ /index.html; } } include /etc/nginx/conf.d/*.conf; }