前端页面刷新404
vue单页因微信分享和自动登录需要,对于URL中存在’#’的地址,处理起来比较坑。用history模式就不会存在这样的问题。但是换成history模式,就会有个新的问题,就是页面刷新后,页面就无法显示了(404)。对于这个问题,我们只需要在服务器配置如果URL匹配不到任何静态资源,就跳转到默认的index.html。
server {
listen 8888;
server_name localhost;
root E:/vue/my_project/dist;
location / {
try_files $uri $uri/ @router;
index index.html index.htm;
}
location @router {
rewrite ^.*$ /index.html last;
}
}
解析远程真实IP
server {
listen 8888;
server_name localhost;
root E:/vue/my_project/dist;
location / {
index index.html index.htm;
proxy_set_header X-Real-IP $remote_addr;
}
}
Windows下nginx
#启动
start nginx
#停止
nginx -s stop
#重启
nginx -s reload
Linux下ningx
#首先利用配置文件启动Nginx:
nginx -c /usr/local/nginx/conf/nginx.conf
重启服务:
service nginx restart
#快速停止或关闭Nginx:
nginx -s stop
#正常停止或关闭Nginx:
nginx -s quit
# 配置文件修改重装载命令:
nginx -s reload
完整配置案例
http {
include 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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
upstream fabric {
server 192.168.1.118:8085;
}
server {
listen 9527;
server_name 192.168.1.118;
client_max_body_size 1000M;
root D:/software/nginx-1.14.0/dist;
location / {
try_files $uri $uri/ @router;
index index.html index.htm;
proxy_set_header X-Real-IP $remote_addr;
}
location @router {
rewrite ^.*$ /index.html last;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}