nginx代理实践(一)多项目本地联调

开发过程中,经常会遇到新老项目交互的场景。项目间共享登录状态和缓存,页面无缝跳转。用着不同的端口,却要和线上有一致的体验。
本文将介绍如何配置nginx以实现在本地进行集成开发。

配置hosts

找到并打开hosts文件:

C:\Windows\System32\drivers\etc\hosts

增加一行:

1
127.0.0.1   local.dev.yitong.com

配置nginx

下载nginx到任意目录:

E:\Program Files\nginx-1.15.4

修改配置文件:

E:\Program Files\nginx-1.15.4\conf\nginx.config

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
81
82

#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;

ssl_certificate E:\cer\fullchain.cer;
ssl_certificate_key E:\cer\_.dev.yitong.com.key;
#ssl_certificate_key E:\cer\_.test.yitong.com.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;

server {

listen 80;
listen 443 ssl http2;
listen 2333;
#server_name _;
server_name localhost;#域名
index index.html index.htm index.php;

# pc-account 项目
location ^~ /vc3/wx/ {
proxy_pass http://10.10.12.57:3002/;
}

# ytwweb 项目
location ^~ /goods/ {
proxy_pass http://10.10.12.57:3001/goods/;
}
location ^~ /courseware/ {
proxy_pass http://10.10.12.57:3001/courseware/;
}
location ^~ /aboutus/ {
proxy_pass http://10.10.12.57:3001/aboutus/;
}
location ^~ /css/g/ {
proxy_pass http://10.10.12.57:3001/css/g/;
}
location ^~ /img/ {
proxy_pass http://10.10.12.57:3001/img/;
}
location ^~ /js/ {
proxy_pass http://10.10.12.57:3001/js/;
}

# pc-account 项目
location / {
proxy_pass http://10.10.12.57:3000;
}
}
}

配置证书

在nginx.config中配置证书路径:

1
2
ssl_certificate E:\cer\fullchain.cer;
ssl_certificate_key E:\cer\_.dev.yitong.com.key;

验证并启动nginx

校验nginx配置是否正确:

1
2
3
4
5
E:\Program Files\nginx-1.15.4>nginx.exe -t
# nginx: the configuration file E:\Program Files\nginx-1.15.4/conf/nginx.conf syntax is ok
# nginx: configuration file E:\Program Files\nginx-1.15.4/conf/nginx.conf test is successful

E:\Program Files\nginx-1.15.4>nginx.exe # 启动

如上所示,若未运行则双击运行 nginx.exe

启动各本地web服务

在nginx.conf中配置不同端口和路径

windows下关闭nginx进程

1
taskkill /f /t /im nginx.exe

nginx代理实践(一)多项目本地联调
http://example.com/20201120-nginx-0/
作者
csorz
发布于
2020年11月20日
许可协议