nginx.conf配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| #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;
# another virtual host using mix of IP-, name-, and port-based configuration
server { # 修改nginx默认端口 listen 81; server_name localhost;
location / { root html; index index.html index.htm; } } include vhosts/*.conf;
# 将9001端口号请求转发到 匹配到的正则表达式 下 # ~ :使用正则匹配,不然就是完全匹配。 server { listen 9001; server_name localhost;
location ~ /eduservice/ { proxy_pass http://localhost:8002; } location ~ /eduoss/ { proxy_pass http://localhost:8003; } location ~ /eduvod/ { proxy_pass http://localhost:8004; } location ~ /cmsservice/ { proxy_pass http://localhost:8005; } location ~ /ucenterservice/ { proxy_pass http://localhost:8006; } location ~ /edumsm/ { proxy_pass http://localhost:8007; } location ~ /orderservice/ { proxy_pass http://localhost:8008; } location ~ /staservice/ { proxy_pass http://localhost:8009; } }
client_max_body_size 50m; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| #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; charset utf-8;
sendfile on;
keepalive_timeout 65; client_max_body_size 120m; upstream tomcat_8080 { ip_hash; server 127.0.0.1:8082; }
upstream tomcat_form_38082 { ip_hash; server 127.0.0.1:38082; } upstream tomcat_form_103 { ip_hash; server 123.456.789.90:8080; }
server { listen 8080; server_name 127.0.0.1;
access_log logs/host.8080.access.log;
location /api/xforms-service { proxy_set_header Host $http_host; proxy_set_header X-Real-Ip $remote_addr; proxy_set_header X-Forwarded-For $remote_addr; proxy_pass http://tomcat_form_38082/api/xforms-service; } location /api/interface-admin-service { proxy_set_header Host $http_host; proxy_set_header X-Real-Ip $remote_addr; proxy_set_header X-Forwarded-For $remote_addr; proxy_pass http://tomcat_form_103/api/interface-admin-service; }
location / { proxy_set_header Host $http_host; proxy_set_header X-Real-Ip $remote_addr; proxy_set_header X-Forwarded-For $remote_addr; proxy_pass http://tomcat_8080; } } }
|


前后端分离部署
第一次部署前后端分离项目时,核心就三步:前端静态资源、后端 jar、Nginx 转发。
前端
1、npm run build
2、把 dist 里的静态文件上传到站点目录
3、如果接口地址写死了本地地址,打包前先改成服务器地址或走 Nginx 代理
后端
1、mvn package 打出 jar
2、把 jar 传到服务器目录
3、确认数据库地址、账号、密码都是线上配置
1 2 3
| nohup java -jar app.jar > app.log 2>&1 & ps -ef | grep java kill -9 pid
|
常见运行方式:
1、java -jar app.jar:当前窗口关闭后进程结束
2、java -jar app.jar &:放到后台,但会受会话影响
3、nohup java -jar app.jar > app.log 2>&1 &:最常用,适合长期运行
一起部署
如果不分开部署,也可以把前端构建产物放进 Spring Boot 的 resources/static。
这种方式的关键不是“能不能跑起来”,而是:
1、静态资源路径是否正确
2、Spring Security 是否拦了首页和静态资源
1 2 3 4 5 6 7
| @Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override public void configure(WebSecurity web) throws Exception { web.ignoring().antMatchers("/login", "/img/**", "/fonts/**", "/css/**", "/index.html", "/favicon.ico"); } }
|
多站点和端口
同一台机器挂多个站点,本质上还是不同 listen 和 server_name。
宝塔多站点
如果是宝塔面板里配多个站点,重点看站点配置文件:
1 2 3 4 5 6 7 8 9
| server { listen 6602; server_name 192.168.1.666;
location / { root /www/wwwroot/site2; index index.html index.htm; } }
|
要点:
1、端口要和站点一一对应
2、域名管理里不要留错的历史域名
3、改完后重载或重启 Nginx
子域名映射到端口
子域名访问某个端口,本质上还是 DNS + 端口放行 + Nginx 监听。
1 2 3 4 5 6 7 8 9 10
| server { listen 8181; server_name www.coderblue.cn;
location / { root /home/git/projects/blog; index index.html index.htm; autoindex on; } }
|
要点:
1、先确认域名已经解析到服务器 IP
2、服务器安全组要放开对应端口
3、访问的是子域名,但真正落点还是监听该端口的 Nginx server
HTTPS
部署完成后,如果还要补 HTTPS,核心就是证书文件、listen 443 ssl 和 80 到 443 的跳转。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| server { listen 443 ssl; server_name www.example.com; ssl_certificate cert/domain.pem; ssl_certificate_key cert/domain.key;
location / { root /home/git/projects/blog; index index.html index.htm; } }
server { listen 80; server_name www.example.com; rewrite ^(.*)$ https://$host$1 permanent; }
|
上线时优先检查:
1、证书路径对不对
2、443 端口有没有开放
3、Nginx 是不是加载了你改的那份配置