nginx请求转发
nginx.conf 配置文件如下:
#user nobody;
worker_processes 1;
error_log logs/error.log error;
#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;
access_log off;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
client_max_body_size 20m;
client_body_buffer_size 128k;
gzip on;
send_timeout 500;
proxy_read_timeout 500;
proxy_connect_timeout 500;
server {
listen 80;
location /ABCBank {
proxy_pass http://173.16.15.7:867/ABCBank;
proxy_redirect off ;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /endpoint {
proxy_pass http://173.16.15.7:867/endpoint;
proxy_redirect off ;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /AbcManagement {
proxy_pass http://173.16.15.7:867/AbcManagement;
proxy_redirect off ;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}
另外一个实例
server {
listen 81;
location /chanjet {
proxy_pass http://10.1.243.55:8080/chanjet;
proxy_redirect off ;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
配置上述文件的ip是: 172.18.22.72
10.1.243.55是我的ip
若蒙访问的地址是: http://172.18.22.72:81/chanjet/workbench/replacemobile/unbindCode?mobile=123
实际上是访问我的电脑
设置nginx 的web目录
把nginx当做静态服务器
server {
listen 80;
location /chanjet {
add_header Access-Control-Allow-Origin *;
root /opt/yunyingyong/mainsite/mainsite/src/web_js/www;
}
location /chanjet/customer {
proxy_pass http://172.18.22.170:8080/chanjet/customer;
#proxy_redirect off ;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location / {
proxy_pass http://172.18.22.170:8080;
#proxy_redirect off ;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
负载均衡
目标:我访问jsp/do/action时,会分配到
localhost:8082
localhost:8080
nginx.conf如下:
#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;
#设定负载均衡的服务器列表
upstream localhost222 {
#weigth参数表示权值,权值越高被分配到的几率越大
server localhost:8082 weight=5;#本机上的Squid开启3128端口
server localhost:8080 weight=5;
}
server {
listen 80;
server_name localhost222;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~* \.(jsp|do|action)$
{
index index.jsp;
proxy_set_header Host $host;
proxy_pass http://localhost222;
proxy_set_header X-Real-IP $remote_addr;
}
}
参考:负载均衡