欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

搭建Nginx七层反向代理

程序员文章站 2022-11-06 23:15:04
基于https://www.cnblogs.com/Dfengshuo/p/11911406.html这个基础上,在来补充下七层代理的配置方式。简单理解下四层和七层协议负载的区别吧,四层是网络层,负载方式也就是IP+端口的方式负载,七层(应用层)协议的负载方式就是通过URL的方式来进行负载。这就是一 ......

基于https://www.cnblogs.com/dfengshuo/p/11911406.html这个基础上,在来补充下七层代理的配置方式。简单理解下四层和七层协议负载的区别吧,四层是网络层,负载方式也就是ip+端口的方式负载,七层(应用层)协议的负载方式就是通过url的方式来进行负载。这就是一个简单通俗的理解吧。

nginx安装方式再讲四层的时候已经说过了,这里就不再继续安装了。就直接上配置文件了

  1 #user  nobody;
  2 worker_processes  1;
  3 
  4 #error_log  logs/error.log;
  5 #error_log  logs/error.log  notice;
  6 #error_log  logs/error.log  info;
  7 #pid        logs/nginx.pid;
  8 events {
  9     worker_connections  1024;
 10 }
 11 
 12 http {
 13     include       mime.types;
 14     default_type  application/octet-stream;
 15     #access_log  logs/access.log  main;
 16     sendfile        on;
 17     #tcp_nopush     on;
 18     #keepalive_timeout  0;
 19     keepalive_timeout  65;
 20     #gzip  on;
 21     server {
 22         listen       80;       #代理端口
 23         server_name  10.156.128.251;    #代理名称
 24         large_client_header_buffers 4 128k;
 25         client_max_body_size 300m;
 26         client_body_buffer_size 128k;
 27         proxy_connect_timeout 600;
 28         proxy_read_timeout 600;
 29         proxy_send_timeout 600;
 30         proxy_buffer_size 64k;
 31         proxy_buffers   4 32k;
 32         proxy_busy_buffers_size 64k;
 33         proxy_temp_file_write_size 64k;
 34         #charset koi8-r;
 35         #access_log  logs/host.access.log  main;
 36         location / {
 37           # root   html;
 38             index  index.html index.htm;
 39             proxy_pass   http://10.156.128.201/;       #要跳转server的ip
 40             proxy_set_header x-real-ip $remote_addr;
 41             proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for;
 42             proxy_set_header host $host:$server_port;
 43 
 44 
 45         }
 46 
 47         #error_page  404              /404.html;
 48 
 49         # redirect server error pages to the static page /50x.html
 50         #
 51         error_page   500 502 503 504  /50x.html;
 52         location = /50x.html {
 53             root   html;
 54         }
 55 
 56         # proxy the php scripts to apache listening on 127.0.0.1:80
 57         #
 58         #location ~ \.php$ {
 59         #    proxy_pass   http://127.0.0.1;
 60         #}
 61 
 62         # pass the php scripts to fastcgi server listening on 127.0.0.1:9000
 63         #
 64         #location ~ \.php$ {
 65         #    root           html;
 66         #    fastcgi_pass   127.0.0.1:9000;
 67         #    fastcgi_index  index.php;
 68         #    fastcgi_param  script_filename  /scripts$fastcgi_script_name;
 69         #    include        fastcgi_params;
 70         #}
 71 
 72         # deny access to .htaccess files, if apache's document root
 73         # concurs with nginx's one
 74         #
 75         #location ~ /\.ht {
 76         #    deny  all;
 77         #}
 78     }
 79 
 80 
 81     # another virtual host using mix of ip-, name-, and port-based configuration
 82     #
 83     #server {
 84     #    listen       8000;
 85     #    listen       somename:8080;
 86     #    server_name  somename  alias  another.alias;
 87 
 88     #    location / {
 89     #        root   html;
 90     #        index  index.html index.htm;
 91     #    }
 92     #}
 93 
 94 
 95     # https server
 96     #
 97     #server {
 98     #    listen       443 ssl;
 99     #    server_name  localhost;
100 
101     #    ssl_certificate      cert.pem;
102     #    ssl_certificate_key  cert.key;
103 
104     #    ssl_session_cache    shared:ssl:1m;
105     #    ssl_session_timeout  5m;
106 
107     #    ssl_ciphers  high:!anull:!md5;
108     #    ssl_prefer_server_ciphers  on;
109 
110     #    location / {
111     #        root   html;
112     #        index  index.html index.htm;
113     #    }
114     #}
115 
116 }

当然,笔者这份配置文件没有进行优化,只是实现了代理效果,自己也可以根据需求进行优化