Nginx 反向代理(一)

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.1719090/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、浏览器访问

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

abel_dwh

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值