一、背景
最近接手了内部的一个接口自动化项目,前后端分开开发的,需要在一台电脑上的调试,所以用Nginx用作转发,统一下端口,记录下。由于系统登录后不是直接跳到接口平台,要跳到一个聚合页面,从聚合页面在点击跳转到接口平台,所以其他的平台在聚合页面展示也一并转发到线上,不影响前端界面展示。
1、我的前端地址:localhost:8082
2、我的后端地址: localhost:9999
3、Nginx转发配置
- worker_processes 1;
-
- events {
- worker_connections 1024;
- }
-
- http {
- include mime.types;
- default_type application/octet-stream;
-
- sendfile on;
- keepalive_timeout 65;
-
- map $http_upgrade $connection_upgrade {
- default keep-alive; #默认为keep-alive 可以支持 一般http请求
- 'websocket' upgrade; #如果为websocket 则为 upgrade 可升级的。
- }
-
- server {
- listen 80;
- server_name localhost;
- location / {
- root html;
- index index.html index.htm;
- }
-
- error_page 500 502 503 504 /50x.html;
- location = /50x.html {
- root html;
- }
- location /atx/{
- proxy_pass http://127.0.0.1:4000/;
- proxy_http_version 1.1;
- proxy_set_header Upgrade $http_upgrade; #此处配置 上面定义的变量
- proxy_set_header Connection $connection_upgrade;
-
- proxy_send_timeout 300;
- proxy_read_timeout 300;
- proxy_connect_timeout 300;
- client_max_body_size 500M;
- }
- location /beantech-report/{
- proxy_pass http://woody.beantechyun.cn/;
- proxy_http_version 1.1;
- proxy_send_timeout 300;
- proxy_read_timeout 300;
- proxy_connect_timeout 300;
- client_max_body_size 500M;
- }
- location ~ /atxprovider/(.*)/(.*)/(.*) {
- try_files /nonexistent @$http_upgrade;
- }
- location @websocket {
- proxy_pass http://$1:$2/$3;
- proxy_http_version 1.1;
- proxy_set_header Upgrade $http_upgrade;
- proxy_set_header Connection "upgrade";
- proxy_set_header Host $host;
- proxy_read_timeout 300;
- }
- location @ {
- # seems not work
- proxy_pass http://$1:$2/$3;
- }
- # for whatsInputAddress
- location ~ /atxprovider/(.*)/(.*) {
- proxy_pass http://$1:$2;
- proxy_http_version 1.1;
- proxy_set_header Upgrade $http_upgrade;
- proxy_set_header Connection "upgrade";
- proxy_set_header Host $host;
- proxy_read_timeout 300;
- }
- }
- #前后端调试配置转发
- server {
- listen 8080;
- server_name localhost;
- location / {
- proxy_pass http://localhost:8082/;
- proxy_http_version 1.1;
- proxy_set_header Upgrade $http_upgrade; #此处配置 上面定义的变量
- proxy_set_header Connection $connection_upgrade;
-
- proxy_send_timeout 300;
- proxy_read_timeout 300;
- proxy_connect_timeout 300;
- client_max_body_size 500M;
- }
-
- error_page 500 502 503 504 /50x.html;
- location = /50x.html {
- root html;
- }
- location /autocenter/{
- proxy_pass http://woody.beantechyun.cn;
- proxy_http_version 1.1;
- proxy_send_timeout 300;
- proxy_read_timeout 300;
- proxy_connect_timeout 300;
- client_max_body_size 500M;
- }
- location /authcenter/{
- proxy_pass http://woody.beantechyun.cn;
- proxy_http_version 1.1;
- proxy_send_timeout 300;
- proxy_read_timeout 300;
- proxy_connect_timeout 300;
- client_max_body_size 500M;
- }
- location /basecenter/{
- proxy_pass http://woody.beantechyun.cn;
- proxy_http_version 1.1;
- proxy_send_timeout 300;
- proxy_read_timeout 300;
- proxy_connect_timeout 300;
- client_max_body_size 500M;
- }
- location /msgcenter/{
- proxy_pass http://woody.beantechyun.cn;
- proxy_http_version 1.1;
- proxy_send_timeout 300;
- proxy_read_timeout 300;
- proxy_connect_timeout 300;
- client_max_body_size 500M;
- }
- location /portalcenter/{
- proxy_pass http://woody.beantechyun.cn;
- proxy_http_version 1.1;
- proxy_send_timeout 300;
- proxy_read_timeout 300;
- proxy_connect_timeout 300;
- client_max_body_size 500M;
- }
- location /testcenter/api/{
- proxy_pass http://127.0.0.1:9999/testcenter/api/;
- proxy_http_version 1.1;
- proxy_set_header Upgrade $http_upgrade; #此处配置 上面定义的变量
- proxy_set_header Connection $connection_upgrade;
- proxy_send_timeout 300;
- proxy_read_timeout 300;
- proxy_connect_timeout 300;
- client_max_body_size 500M;
- }
- location /testcenter/{
- proxy_pass http://127.0.0.1:8082/;
- proxy_http_version 1.1;
- proxy_send_timeout 300;
- proxy_read_timeout 300;
- proxy_connect_timeout 300;
- client_max_body_size 500M;
- }
- location /beantech-report/{
- proxy_pass http://woody.beantechyun.cn/;
- proxy_http_version 1.1;
- proxy_send_timeout 300;
- proxy_read_timeout 300;
- proxy_connect_timeout 300;
- client_max_body_size 500M;
- }
- location ~ /atxprovider/(.*)/(.*)/(.*) {
- try_files /nonexistent @$http_upgrade;
- }
- location @websocket {
- proxy_pass http://$1:$2/$3;
- proxy_http_version 1.1;
- proxy_set_header Upgrade $http_upgrade;
- proxy_set_header Connection "upgrade";
- proxy_set_header Host $host;
- proxy_read_timeout 300;
- }
- location @ {
- # seems not work
- proxy_pass http://$1:$2/$3;
- }
- # for whatsInputAddress
- location ~ /atxprovider/(.*)/(.*) {
- proxy_pass http://$1:$2;
- proxy_http_version 1.1;
- proxy_set_header Upgrade $http_upgrade;
- proxy_set_header Connection "upgrade";
- proxy_set_header Host $host;
- proxy_read_timeout 300;
- }
- }
- server {
- listen 8889;
- server_name localhost;
- location / {
- proxy_pass http://woody.beantechyun.cn/;
- proxy_http_version 1.1;
- proxy_set_header Upgrade $http_upgrade; #此处配置 上面定义的变量
- proxy_set_header Connection $connection_upgrade;
-
- proxy_send_timeout 300;
- proxy_read_timeout 300;
- proxy_connect_timeout 300;
- client_max_body_size 500M;
- }
-
- error_page 500 502 503 504 /50x.html;
- location = /50x.html {
- root html;
- }
- }
- server {
- listen 8891;
- server_name localhost;
- location / {
- proxy_pass http://woody.beantechyun.cn/;
- proxy_http_version 1.1;
- proxy_set_header Upgrade $http_upgrade; #此处配置 上面定义的变量
- proxy_set_header Connection $connection_upgrade;
-
- proxy_send_timeout 300;
- proxy_read_timeout 300;
- proxy_connect_timeout 300;
- client_max_body_size 500M;
- }
-
- error_page 500 502 503 504 /50x.html;
- location = /50x.html {
- root html;
- }
- }
- server {
- listen 8888;
- server_name localhost;
- location / {
- proxy_pass http://woody.beantechyun.cn/;
- proxy_http_version 1.1;
- proxy_set_header Upgrade $http_upgrade; #此处配置 上面定义的变量
- proxy_set_header Connection $connection_upgrade;
-
- proxy_send_timeout 300;
- proxy_read_timeout 300;
- proxy_connect_timeout 300;
- client_max_body_size 500M;
- }
-
- error_page 500 502 503 504 /50x.html;
- location = /50x.html {
- root html;
- }
- }
- }