1.Vue路由打包方式
(1)hash方式
即地址栏后面带#号的方式。
可以设置vue.config.js或者config.js,总之就是配置vue的地方。一般长这样:
首选设置:publicPath,可以使用./设置相对路径,这样打包完的静态文件放到网站任何位置都可以,此时设置src/routers/index.js文件,设置路由模式为hash,如下:
export default new Router({
mode: 'hash', // 去掉url中的#
scrollBehavior: () => ({ y: 0 }),
routes: constantRoutes
})
(2)history方式
即地址栏中显示的path路径,拼接方式为:主路径+路由路径
这种方式下,publicPath必须使用相对站点根目录的路径方式,
例如:https://www.baidu.com/vue/test/index.html,像这样的地址路径,则publicPath必须设置为/vue/test
同时需要设置路由history模式并设置base为/vue/test:
export default new Router({
mode: 'history', // 去掉url中的#
base: '/vue/test',//根目录相对路径
scrollBehavior: () => ({ y: 0 }),
routes: constantRoutes
})
2.nginx配置
(1)配置转发
对于hash模式,nginx只需要配置静态路径的转发即可。但是对history需要做如下转发设置。
server {
listen 80;
server_name localhost;
# 非根目录的转发
location ^~ /vue/test {
index index.html index.htm;
alias html/admin;
try_files $uri $uri/ /admin/index.html;
}
# 根目录的转发
location / {
root html;
index index.html index.htm;
add_header Cache-Control no-store;
if (!-e $request_filename) {
rewrite ^(.*)$ /index.html?s=$1 last;
break;
}
}
}
(2)开启GZIP压缩
gzip on;
gzip_http_version 1.1;
gzip_vary on;
gzip_comp_level 6;
gzip_proxied any;
gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;
gzip_min_length 1k;
gzip_buffers 16 8k;
gzip_disable "MSIE [1-6]\.";
(3)完整配置文件
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
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;
server_tokens off;
proxy_hide_header X-Application-Context;
proxy_hide_header Server;
gzip on;
gzip_http_version 1.1;
gzip_vary on;
gzip_comp_level 6;
gzip_proxied any;
gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;
gzip_min_length 1k;
gzip_buffers 16 8k;
gzip_disable "MSIE [1-6]\.";
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location ^~ /prod-api {
proxy_pass http://127.0.0.1:8080;
index index.html index.htm;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
set $realip $remote_addr;
if ($http_x_forwarded_for ~ "^(\d+\.\d+\.\d+\.\d+)") {
set $realip $1;
}
fastcgi_param REMOTE_ADDR $realip;
}
location ^~ /admin {
index index.html index.htm;
alias html/admin;
try_files $uri $uri/ /admin/index.html;
}
location / {
root html;
index index.html index.htm;
add_header Cache-Control no-store;
if (!-e $request_filename) {
rewrite ^(.*)$ /index.html?s=$1 last;
break;
}
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}