Nginx反向代理
环境
代理服务器 192.168.133.161
web服务 192.168.133.160
代理端:
# vim /etc/nginx/nginx.conf
worker_processes 1;
error_log logs/error.log;
worker_rlimit_nofile 102400;
pid /var/run/nginx.pid;
events {
worker_connections 10240;
use epoll;
}
http {
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 /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
gzip on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
include /etc/nginx/conf.d/*.conf;
}
# 代理服务器指向www.lisi.com(192.168.133.160:8080)
[root@nginx-proxy conf.d]# cat proxy.conf
server {
listen 80;
server_name www.proxy.com;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
location / {
proxy_pass http://192.168.133.160:8080;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-NginX-Proxy true;
proxy_connect_timeout 30;
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_buffering on;
proxy_buffer_size 32k;
proxy_buffers 4 128k;
proxy_busy_buffers_size 256k;
proxy_max_temp_file_size 256k;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
# 代理服务器指向www.wangwu.com(192.168.133.160:80)
[root@nginx-proxy conf.d]# cat proxy-test.conf
server {
listen 80;
server_name www.proxy-test.com;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
location / {
proxy_pass http://192.168.133.160:80;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-NginX-Proxy true;
proxy_connect_timeout 30;
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_buffering on;
proxy_buffer_size 32k;
proxy_buffers 4 128k;
proxy_busy_buffers_size 256k;
proxy_max_temp_file_size 256k;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
服务端
[root@nginx-server conf.d]# cat lisi-query.conf
server {
listen 8080;
server_name www.lisi.com;
access_log /var/log/nginx/lisi-access.log main;
error_log /var/log/nginx/lisi-error.log;
set_real_ip_from 192.168.133.161;
location /{
root /var/www/lisi;
index index.html index.htm index.php;
deny 192.168.133.5;
allow all;
}
location /status{
stub_status on;
access_log off;
}
}
[root@nginx-server conf.d]# cat proxy.conf
server {
listen 192.168.133.160:80;
server_name www.wangwu.com;
access_log /var/log/nginx/wangwu-access.log main;
error_log /var/log/nginx/wangwu-error.log;
set_real_ip_from 192.168.133.161;
location /{
root /var/www/test;
index index.html index.htm index.php;
}
location /status{
stub_status on;
access_log off;
}
}