文章目录
- 一、快速入门
- 1. 背景
- 2. 案例1
- 3. 案例2
- 二、实战
- 2.1. 配置备份
- 2.2. 配置清空
- 2.3. 配置初始化
- 2.4. 配置统一管理
- 2.5. 开发配置
- 2.6. UAT配置
- 2.7. 启动nginx
一、快速入门
1. 背景
使用nginx做反向代理的时候,可以简单的直接把请求原封不动的转发给下一个服务。设置proxy_pass请求只会替换域名,如果要根据不同的url后缀来访问不同的服务,则需要通过如下方法:
2. 案例1
server {listen 8000;server_name abc.com;access_log "pipe:rollback /data/log/nginx/access.log interval=1d baknum=7 maxsize=1G" main;location ^~/user/ {proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-NginX-Proxy true;proxy_pass http://user/;}location ^~/order/ {proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-NginX-Proxy true;proxy_pass http://order/;}
}
^~/user/表示匹配前缀是user的请求,proxy_pass的结尾有/, 则会把/user/*后面的路径直接拼接到后面,即移除user。
3. 案例2
upstream user {server localhost:8089 weight=5;
}
upstream order {server localhost:8090 weight=5;
}server {listen 80;server_name abc.com;access_log "pipe:rollback /data/log/nginx/access.log interval=1d baknum=7 maxsize=1G" main;location ^~/user/ {proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-NginX-Proxy true;rewrite ^/user/(.*)$ /$1 break;proxy_pass http://user;}location ^~/order/ {proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-NginX-Proxy true;rewrite ^/order/(.*)$ /$1 break;proxy_pass http://order;}
}
proxy_pass结尾没有/, rewrite重写了url。
二、实战
2.1. 配置备份
cd /usr/local/nginx/conf/
cp nginx.conf nginx.conf.bak
2.2. 配置清空
清空nginx.conf文件内容
cat /dev/null > nginx.conf
2.3. 配置初始化
添加以下内容
user ec2-user ec2-user;
worker_processes 1; 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; tcp_nodelay on; keepalive_timeout 65; include /usr/local/nginx/conf/conf.d/*.conf;
}
2.4. 配置统一管理
在conf目录下面创建conf.d文件夹,用于存储永不同环境的配置文件
mkdir conf.d
2.5. 开发配置
添加 开发环境配置文件dev.conf
cd conf.d
vim dev.conf
添加以下内容
##############################################DEV环境##################################################
server { listen 80; server_name dev-ams.gblfy.com.cn; location / { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-NginX-Proxy true; proxy_pass http://xxx.xxx.xxx.xxx:8082; } location ^~/dev-api/ { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-NginX-Proxy true; rewrite ^/dev-api/(.*)$ /$1 break; proxy_pass http://xxx.xxx.xxx.xxx:8080; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
2.6. UAT配置
添加 uat环境配置文件uat.conf
cd conf.d
vim uat.conf
内容详细如下
###############################################UAT环境##################################################
upstream app-cluster-test { server xxx.xxx.xxx.xx1:8080; server xxx.xxx.xxx.xx2:8080; } server { listen 80; server_name uat-ams.gblfy.com.cn; location / { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-NginX-Proxy true; proxy_pass http://xxx.xxx.xxx.xxx:8082; } location ^~/uat-api/ { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-NginX-Proxy true; rewrite ^/uat-api/(.*)$ /$1 break; proxy_pass http://app-cluster-test; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
2.7. 启动nginx
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf