关于Nginx前后端分离部署spring boot和vue工程以及反向代理的配置说明
最近项目中用到关于Nginx前后端分离部署spring boot和vue工程以及反向代理的配置,总结了一下说明:
1、后端是spring boot工程,端口8000,通过 jar命令启动
nohup java -jar xxx-jsonflow-biz.jar > /usr/local/nohup.out 2>&1 &
2、后台前端是vue工程,打包成dist文件夹,端口是13000,nginx配置如下:
server {
listen 13000;
server_name localhost;
gzip on;
gzip_min_length 1k;
gzip_comp_level 4;
gzip_proxied any;
gzip_types text/plain text/xml text/css;
gzip_vary on;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
root /usr/local/nginx/html/dist;
location ~* ^/jsonflow/ {
#proxy_pass http://127.0.0.1:8000;
proxy_connect_timeout 15s;
proxy_send_timeout 15s;
proxy_read_timeout 15s;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto http;
#proxy_method GET POST PUT DELETE;
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Credentials 'true';
add_header Access-Control-Allow-Methods 'GET,PUT,POST,OPTIONS';
add_header Access-Control-Allow-Headers 'user-id,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
if ($request_method = 'OPTIONS') {
return 204;
}
proxy_pass http://127.0.0.1:8000;
}
if ($request_uri ~ "/actuator"){
return 403;
}
}
3、h5移动端是2个应用,配了2个server,分别对应h5文件夹下的2个目录,nginx配置如下:
server {
listen 9001;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
error_page 403 =200 /40x.html;
location /40x.html {
root html;
}
location / {
root html/h5/yiyuan;
index index.html ;
}
}
server {
listen 9002;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
error_page 403 =200 /40x.html;
location /40x.html {
root html;
}
location / {
root html/h5/yaodian;
index index.html ;
}
}
4、完整的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 {
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
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 {
listen 9001;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
error_page 403 =200 /40x.html;
location /40x.html {
root html;
}
location / {
root html/h5/yiyuan;
index index.html ;
}
}
server {
listen 9002;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
error_page 403 =200 /40x.html;
location /40x.html {
root html;
}
location / {
root html/h5/yaodian;
index index.html ;
}
}
server {
listen 13000;
server_name localhost;
gzip on;
gzip_min_length 1k;
gzip_comp_level 4;
gzip_proxied any;
gzip_types text/plain text/xml text/css;
gzip_vary on;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
root /usr/local/nginx/html/dist;
location ~* ^/(jsonflow/ws) {
proxy_pass http://127.0.0.1:8000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
location ~* ^/jsonflow/ {
#proxy_pass http://127.0.0.1:8000;
proxy_connect_timeout 15s;
proxy_send_timeout 15s;
proxy_read_timeout 15s;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto http;
#proxy_method GET POST PUT DELETE;
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Credentials 'true';
add_header Access-Control-Allow-Methods 'GET,PUT,POST,OPTIONS';
add_header Access-Control-Allow-Headers 'user-id,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
if ($request_method = 'OPTIONS') {
return 204;
}
proxy_pass http://127.0.0.1:8000;
}
location ~* ^/(gen) {
proxy_pass http://127.0.0.1:5003;
proxy_connect_timeout 15s;
proxy_send_timeout 15s;
proxy_read_timeout 15s;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto http;
}
if ($request_uri ~ "/actuator"){
return 403;
}
}
}
5、nginx目录如下图:
6、番外,关于https证书的配置:
server {
listen 443 ssl;
server_name localhost;
gzip on;
gzip_static on;
gzip_min_length 1k;
gzip_comp_level 4;
gzip_proxied any;
gzip_types text/plain text/xml text/css;
gzip_vary on;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
ssl_protocols GMTLSv1.1;
ssl_certificate crt/gm/SS.pem;
ssl_certificate_key crt/gm/SS.key.pem;
ssl_certificate crt/gm/SE.pem;
ssl_certificate_key crt/gm/SE.key.pem;
root /usr/local/dist;
location ~* ^/(ewaycloud) {
proxy_pass http://127.0.0.1:8000;
proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto http;
}
}