場景
項(xiàng)目需要區(qū)分系統(tǒng)使用者身份,存在諸如xxx/admin 、 xxx/client這樣的訪問地址。 因此在需要進(jìn)行對(duì)相應(yīng)項(xiàng)目進(jìn)行獨(dú)立部署
部署環(huán)境
os: mac(10.12.6)
服務(wù)器: Nginx (1.17.1)
前端項(xiàng)目開發(fā)框架: Umi (2.8.11)
解決方案一:(nginx指定靜態(tài)資源路徑)
項(xiàng)目配置
Umi中config配置(config/config.js)添加如下參數(shù)
base: '/admin' (如果為client端,相應(yīng)為base: '/client');
參考;https://umijs.org/zh/guide/deploy.html#%E9%83%A8%E7%BD%B2-html-%E5%88%B0%E9%9D%9E%E6%A0%B9%E7%9B%AE%E5%BD%95
原理請(qǐng)查閱react-router的basename
Nginx配置
Nginx (www)文件結(jié)構(gòu)如下
├── 50x.html
├──admin
│ ├──index.html
│ ├──latout.css
│ ├──main.js
├──client
│ ├──index.html
│ ├──latout.css
│ ├──main.js
Nginx配置如下
#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;
#gzip on;
server {
listen 9898;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
# proxy_set_header Host $host:$server_port;
# 重點(diǎn):解決二級(jí)目錄部署時(shí),訪問css、js等靜態(tài)資源報(bào)404的問題
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|css|js|eot|svg|ttf|woff|woff2|properties|json)$ {
# root /usr/local/var/www/admin;
if (-f $request_filename) {
expires 1d;
break;
}
}
location /admin {
alias /usr/local/var/www/admin;
# 重點(diǎn):解決刷新后404問題
try_files $uri /admin/index.html;
index index.html index.htm;
}
location /client {
alias /usr/local/var/www/client;
# 重點(diǎn):解決刷新后404問題
try_files $uri /client/index.html;
index index.html index.htm;
}
location /api {
proxy_pass http://192.168.110.158:3001/api/v1;
}
#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 /usr/local/var/www/50x.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;
# }
#}
include servers/*;
}
解決方案二(webpack配置publicPath)
1、 項(xiàng)目配置
base: '/admin/',
publicPath: '/admin/',
2、移除Nginx中靜態(tài)資源配置項(xiàng): location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|css|js|eot|svg|ttf|woff|woff2|properties|json)$ {...}
解釋:
base做為路由路徑,為匹配nginx的 location ^~ /admin{ }
publicPath為靜態(tài)資源指定路徑,打包出來css與js引入路徑如圖所示:

css.jpg

js.jpg
su.
參考本文可解決:SPA應(yīng)用部署后刷新頁面404問題,css/js/png等靜態(tài)文件無法訪問,或訪問404問題
參看本文原則上可解決Nginx多層級(jí)目錄部署問題;