开发过程中,经常会遇到新老项目交互的场景。项目间共享登录状态和缓存,页面无缝跳转。用着不同的端口,却要和线上有一致的体验。 本文将介绍如何配置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 worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; ssl_certificate E:\cer\fullchain.cer; ssl_certificate_key E:\cer\_.dev.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 localhost; index index.html index.htm index.php; location ^~ /vc3/wx/ { proxy_pass http://10.10.12.57:3002/; } 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/; } 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 E:\Program Files\nginx-1.15.4>nginx.exe
如上所示,若未运行则双击运行 nginx.exe
启动各本地web服务 在nginx.conf中配置不同端口和路径
windows下关闭nginx进程 1 taskkill /f /t /im nginx.exe