nginx的安装:源码、YUM的方式请参考:https://blog.csdn.net/abel_dwh/article/details/90179053
1、介绍
反向代理(Reverse Proxy)方式是指以代理服务器来接受Internet上的连接请求,然后将请求转发给内部网络上的服务器;并将从服务器上得到的结果返回给Internet上请求连接的客户端,此时代理服务器对外就表现为一个服务器。
通常的代理服务器,只用于代理内部网络对Internet的连接请求,客户机必须指定代理服务器,并将本来要直接发送到Web服务器上的http请求发送到代理服务器中。当一个代理服务器能够代理外部网络上的主机,访问内部网络时,这种代理服务的方式称为反向代理服务。
2、部署ip及目录
本次部署采用Nginx代理后端tomcat Web服务器
名称 | 地址 | 端口 | 目录 |
代理服务器 | 192.168.122.171 | 9090 | /usr/local/tomcat/webapps/test/index.jsp #请求页面地址 |
web服务器 | 192.168.122.165 10.10.10.10 | 50010 | /usr/local/nginx/vhost/tomcat.conf #配置文件目录 /usr/local/nginx/nginx.conf |
3、Nginx的主配置文件:日志格式设定支持打印POST
[root@localhost nginx]# pwd
/usr/local/nginx
[root@localhost nginx]# more nginx.conf
user www www;
worker_processes 1;
error_log logs/error.log crit;
pid logs/nginx.pid;
events {
use epoll;
multi_accept on;
worker_connections 1024;
}
http {
server_tokens off;
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 128;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
# 连接超时时间
keepalive_timeout 10;
# 服务端在读取客户端发送的请求时设置的超时时间
client_header_timeout 10;
client_body_timeout 10;
reset_timedout_connection on;
# 设置服务端传送回应包时的超时时间
send_timeout 10;
client_max_body_size 30m;
client_body_buffer_size 512k;
client_header_buffer_size 512k;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
output_buffers 256 1024k;
#open_file_cache 打开缓存的同时也指定了缓存最大数目,以及缓存的时间。我们可以设置一个相对高的最大时间,这样我们可以在它们不活动超过20秒后清除掉。
#open_file_cache_valid 在open_file_cache中指定检测正确信息的间隔时间。
#open_file_cache_min_uses 定义了open_file_cache中指令参数不活动时间期间里最小的文件数。
#open_file_cache_errors 指定了当搜索一个文件时是否缓存错误信息,也包括再次给配置中添加文件。我们也包括了服务器模块,这些是在不同文件中定义的。如果你的服务器模块不在这些位置,你就得修改这一行来指定正确的位置。
open_file_cache max=100000 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
open_file_cache_errors on;
# 日志格式设定
log_format main escape=json '{ "@timestamp": "$time_iso8601", ' '"remote_addr": "$remote_addr",' '"costime
": "$request_time",' '"realtime": "$upstream_response_time",' '"status": $status,' '"x_forwarded": "$http_x_fo
rwarded_for",' '"referer": "$http_referer",' '"request": "$request",' '"upstr_addr": "$upstream_addr",' '"byte
s":$body_bytes_sent,' '"dm":$request_body,' '"agent": "$http_user_agent" }';
#虚拟主机配置文件,以conf后缀
include vhost/*.conf;
}
打印的日志格式
4、反向代理简单配置
vim /usr/local/nginx/vhost/tomcat.conf
upstream tomcat {
server 192.168.122.171:9090;#这是被代理端,真是的访问地址
}
server {
listen 50010; #代理服务器监听的端口
server_name www.dwh.test; #域名
access_log logs/tomcat_access.log main; #日志存储目录
error_log logs/tomcat_erro.log;
root html;
index index.html index.htm index.php;
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Real-Port $remote_port;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Headers X-Requested-With;
add_header Access-Control-Allow-Methods GET,POST; #支持客户端的请求方法。post/get;
proxy_pass http://tomcat/test/;
}
}
5、浏览器访问