1、前端配置
将打包后的dist内容放入/usr/local/nginx/下的html文件夹中
location / {
# root html
try_files $uri $uri/ index.html
index indx.html index.htm
}
2、后端接口代理配置 (以/server01为例,后端ip及端口为http://localhost:6000)
location ~/server01/{
rewrite ^/server01/(.*)$ $1 break;
proxy_pass http://localhost:6000;
}
3、完整代码如下:
#配置用户或者组,默认为nobody nobody。
user nginx;
#允许生成的进程数,默认为1
worker_processes auto;
#制定日志路径,级别。这个设置可以放入全局块,http块,server块,级别以此为:debug|info|notice|warn|error|crit|alert|emerg
error_log /var/log/nginx/error.log notice;
#指定nginx进程运行文件存放地址
pid /var/run/nginx.pid;
events {
accept_mutex on; #设置网路连接序列化,防止惊群现象发生,默认为on
multi_accept on; #设置一个进程是否同时接受多个网络连接,默认为off
#use epoll; #事件驱动模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport
#最大连接数,默认为512
worker_connections 1024;
}
http {
#文件扩展名与文件类型映射表
include /etc/nginx/mime.types;
#默认文件类型,默认为text/plain
default_type application/octet-stream;
#给文件格式命名为main
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
# 配置nginx 日志地址,并设置文件格式为mian
access_log /var/log/nginx/access.log main;
#允许sendfile方式传输文件,默认为off,可以在http块,server块,location块。
sendfile on;
#tcp_nopush on;
#连接超时时间,默认为75s,可以在http,server,location块。
keepalive_timeout 65;
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Method' 'GET,POST,DELETE,PUT';
add_header 'Access-Control-Allow-Headers' 'X-Requested-With';
server {
# 表示 nginx 监听的端口
listen 80;
//前端配置
location / {
# root html
try_files $uri $uri/ index.html
index indx.html index.htm
}
//后端接口代理配置 以server01为例
location ~/server01/{
rewrite ^/server01/(.*)$ $1 break;
proxy_pass http://localhost:6000;
}
error_page 404 /404.html;
location = /40x.html {
root html
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html
}
}
}