AngularJS / Vue / React 前端站点如果使用了单页面模式,在开发模式下直接使用 url 路由访问是正常的,但是在 nginx 下面会直接报 404 错误。
解决方式是加入 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;
}