本文前提是 windows环境,linux环境类似
首先,已经编写好两个vue工程,打包获得了两个dist文件夹,
将他们放在nginx/html中,如下图
然后,就是最重要的nginx.conf文件的编写
#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 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
# 这个决定了在localhost后面跟的是什么
location / {
# 这是root,下面是alias
root D:/software/nginx-1.22.0/html/employee;
try_files $uri $uri/ /dist2/index.html;
index index.html index.html;
}
# localhost/admin
location /admin {
alias D:/software/nginx-1.22.0/html/admin;
try_files $uri $uri/ /dist2/index.html;
index index.html index.html;
}
#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;
}
# 反向代理,处理管理端发送的请求
# 只有这个才决定了,把前端请求的网址,转换成什么
# 也就是说,把request.js中的 localhost/api/xxxx=>proxy_pass/xxxx
location /api/ {
proxy_pass http://localhost:8080/;
#proxy_pass http://webservers/admin/;
}
# 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;
# }
#}
}
所以,上面有用的地方就三个,一个是监听的端口,一个是浏览器中的地址,再一个就是反向代理。
最后的最后,不要忘记,重载nginx。
nginx -s reload