• 技术文章 >后端开发 >php教程

    一个nignx 服务反向代理3个IP访问的配置方法

    2016-08-08 09:21:23原创593

    场景:在10.17.55,44:7002和10.17.55.46:7003的weblogic 部署一个web工程,准备做负载均衡。在一台IP:12.17.56.44能访问到55.44和55.46上web工程的机器做nginx代理服务器,代理端口为7000,既12.17.56.44:7000能访问到10.17.55,44:7002或10.17.55.46:7003的web工程,而互联网有个IP22.18.55.33:7000 可以访问到,12.17.56.44:7000,12.17.56.44这台服务器的nginx.conf 内容配置如下:

    #user  nobody;
    worker_processes  auto;
    
    #error_log  logs/error.log;
    #error_log  logs/error.log  notice;
    #error_log  logs/error.log  info;
    
    #pid        logs/nginx.pid;
    
    
    events {
        worker_connections  2048;
        accept_mutex off;
        multi_accept on; 
        use epoll; 
    }
    
    # load modules compiled as Dynamic Shared Object (DSO)
    #
    #dso {
    #    load ngx_http_fastcgi_module.so;
    #    load ngx_http_rewrite_module.so;
    #}
    #thread_pool one threads=128 max_queue=0;
    http {
        include       mime.types;
        default_type  application/octet-stream;
        #thread_pool one threads=128 max_queue=0;
    	include /etc/nginx/mutnginx/*.conf; 
        #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;
        sendfile_max_chunk 512k;
        #keepalive_timeout  0;
        keepalive_timeout  65;
    
       upstream cluster1 {
            # simple round-robin
            ip_hash;
    		server  10.17.55,44:7002;
            server  10.17.55.46:7003;
    
          
        }
      
      #gzip  on;
    
        server {
            listen       7000;
    		server_name    localhost;
        	#   0.99.132.52:700
    		#check interval=30
            #charset koi8-r;
    
            #access_log  logs/host.access.log  main;
    
            location / {
      
          	#aio threads=one; 
            proxy_set_header Host $host:7000;
            proxy_set_header X-Real-IP $remote_addr;
    	proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            
          	proxy_buffering off;
            proxy_pass http://cluster1/;
    		   # root   html;
               #index  index.html index.htm;
            }
    
    	 location /status {
                #check_status;
    
                access_log   off;
                allow all;
               # deny all;
            }
    
            #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;
            }
    
            # proxy the PHP scripts to Apache listening on 127.0.0.1:80
            #
            #location ~ \.php$ {
            #    proxy_pass   http://127.0.0.1;
            #}
    
            # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
            #
            #location ~ \.php$ {
            #    root           html;
            #    fastcgi_pass   127.0.0.1:9000;
            #    fastcgi_index  index.php;
            #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            #    include        fastcgi_params;
            #}
    
            # deny access to .htaccess files, if Apache's document root
            # concurs with nginx's one
            #
            #location ~ /\.ht {
            #    deny  all;
            #}
        }
    
    
        # another virtual host using mix of IP-, name-, and port-based configuration
        #
        #server {
        #    listen       8000;
        #    listen       somename:8080;
        #    server_name  somename  alias  another.alias;
    
        #    location / {
        #        root   html;
        #        index  index.html index.htm;
        #    }
        #}
    
    
        # HTTPS server
        #
        #server {
        #    listen       443;
        #    server_name  localhost;
    
        #    ssl                  on;
        #    ssl_certificate      cert.pem;
        #    ssl_certificate_key  cert.key;
    
        #    ssl_session_timeout  5m;
    
        #    ssl_protocols  SSLv2 SSLv3 TLSv1;
        #    ssl_ciphers  HIGH:!aNULL:!MD5;
        #    ssl_prefer_server_ciphers   on;
    
        #    location / {
        #        root   html;
        #        index  index.html index.htm;
        #    }
        #}
    
    }
    

    配置文件中:

    include /etc/nginx/mutnginx/*.conf; 
    是在mutnginx目录下,创建了两个跟nginx.conf 相似的文件。

    nginx2.conf配置是为了让同个网段即12.17.56.*的其他应用能同个代理ip访问到web工程,其配置如下:

    #user  nobody;
    worker_processes  auto;
    
    #error_log  logs/error.log;
    #error_log  logs/error.log  notice;
    #error_log  logs/error.log  info;
    
    #pid        logs/nginx.pid;
    
    
    events {
        worker_connections  2048;
        accept_mutex off;
        multi_accept on; 
        use epoll; 
    }
    
    # load modules compiled as Dynamic Shared Object (DSO)
    #
    #dso {
    #    load ngx_http_fastcgi_module.so;
    #    load ngx_http_rewrite_module.so;
    #}
    #thread_pool one threads=128 max_queue=0;
    http {
        include       mime.types;
        default_type  application/octet-stream;
        #thread_pool one threads=128 max_queue=0;
     
        #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;
        sendfile_max_chunk 512k;
        #keepalive_timeout  0;
        keepalive_timeout  65;
    
       upstream cluster3 {
            # simple round-robin
            ip_hash;
    		server  10.17.55,44:7002;
            server  10.17.55.46:7003;
    
          
        }
      
      #gzip  on;
    
        server {
            listen       7000;
    		server_name    localhost;
        	#   0.99.132.52:700
    		#check interval=30
            #charset koi8-r;
    
            #access_log  logs/host.access.log  main;
    
            location / {
      
          	#aio threads=one; 
            proxy_set_header Host $host:7000;
            proxy_set_header X-Real-IP $remote_addr;
    	proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            
          	proxy_buffering off;
            proxy_pass http://cluster3/;
    		   # root   html;
               #index  index.html index.htm;
            }
    
    	 location /status {
                #check_status;
    
                access_log   off;
                allow all;
               # deny all;
            }
    
            #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;
            }
    
            # proxy the PHP scripts to Apache listening on 127.0.0.1:80
            #
            #location ~ \.php$ {
            #    proxy_pass   http://127.0.0.1;
            #}
    
            # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
            #
            #location ~ \.php$ {
            #    root           html;
            #    fastcgi_pass   127.0.0.1:9000;
            #    fastcgi_index  index.php;
            #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            #    include        fastcgi_params;
            #}
    
            # deny access to .htaccess files, if Apache's document root
            # concurs with nginx's one
            #
            #location ~ /\.ht {
            #    deny  all;
            #}
        }
    
    
        # another virtual host using mix of IP-, name-, and port-based configuration
        #
        #server {
        #    listen       8000;
        #    listen       somename:8080;
        #    server_name  somename  alias  another.alias;
    
        #    location / {
        #        root   html;
        #        index  index.html index.htm;
        #    }
        #}
    
    
        # HTTPS server
        #
        #server {
        #    listen       443;
        #    server_name  localhost;
    
        #    ssl                  on;
        #    ssl_certificate      cert.pem;
        #    ssl_certificate_key  cert.key;
    
        #    ssl_session_timeout  5m;
    
        #    ssl_protocols  SSLv2 SSLv3 TLSv1;
        #    ssl_ciphers  HIGH:!aNULL:!MD5;
        #    ssl_prefer_server_ciphers   on;
    
        #    location / {
        #        root   html;
        #        index  index.html index.htm;
        #    }
        #}
    
    }
    

    还有一个nginx3.conf 是为了让,其他内网ip统一访问的代理,其配置如下:

    #user  nobody;
    worker_processes  auto;
    
    #error_log  logs/error.log;
    #error_log  logs/error.log  notice;
    #error_log  logs/error.log  info;
    
    #pid        logs/nginx.pid;
    
    
    events {
        worker_connections  2048;
        accept_mutex off;
        multi_accept on; 
        use epoll; 
    }
    
    # load modules compiled as Dynamic Shared Object (DSO)
    #
    #dso {
    #    load ngx_http_fastcgi_module.so;
    #    load ngx_http_rewrite_module.so;
    #}
    #thread_pool one threads=128 max_queue=0;
    http {
        include       mime.types;
        default_type  application/octet-stream;
        #thread_pool one threads=128 max_queue=0;
     
        #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;
        sendfile_max_chunk 512k;
        #keepalive_timeout  0;
        keepalive_timeout  65;
    
       upstream cluster2 {
            # simple round-robin
            ip_hash;
    		server  10.17.55,44:7002;
            server  10.17.55.46:7003;
    
          
        }
      
      #gzip  on;
    
        server {
            listen       7000;
    		server_name    localhost;
        	#   0.99.132.52:700
    		#check interval=30
            #charset koi8-r;
    
            #access_log  logs/host.access.log  main;
    
            location / {
      
          	#aio threads=one; 
            proxy_set_header Host $host:5123;
            proxy_set_header X-Real-IP $remote_addr;
    	proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            
          	proxy_buffering off;
            proxy_pass http://cluster2/;
    		   # root   html;
               #index  index.html index.htm;
            }
    
    	 location /status {
                #check_status;
    
                access_log   off;
                allow all;
               # deny all;
            }
    
            #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;
            }
    
            # proxy the PHP scripts to Apache listening on 127.0.0.1:80
            #
            #location ~ \.php$ {
            #    proxy_pass   http://127.0.0.1;
            #}
    
            # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
            #
            #location ~ \.php$ {
            #    root           html;
            #    fastcgi_pass   127.0.0.1:9000;
            #    fastcgi_index  index.php;
            #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            #    include        fastcgi_params;
            #}
    
            # deny access to .htaccess files, if Apache's document root
            # concurs with nginx's one
            #
            #location ~ /\.ht {
            #    deny  all;
            #}
        }
    
    
        # another virtual host using mix of IP-, name-, and port-based configuration
        #
        #server {
        #    listen       8000;
        #    listen       somename:8080;
        #    server_name  somename  alias  another.alias;
    
        #    location / {
        #        root   html;
        #        index  index.html index.htm;
        #    }
        #}
    
    
        # HTTPS server
        #
        #server {
        #    listen       443;
        #    server_name  localhost;
    
        #    ssl                  on;
        #    ssl_certificate      cert.pem;
        #    ssl_certificate_key  cert.key;
    
        #    ssl_session_timeout  5m;
    
        #    ssl_protocols  SSLv2 SSLv3 TLSv1;
        #    ssl_ciphers  HIGH:!aNULL:!MD5;
        #    ssl_prefer_server_ciphers   on;
    
        #    location / {
        #        root   html;
        #        index  index.html index.htm;
        #    }
        #}
    
    }
    
    其中
    proxy_set_header Host $host:5123;#是映射端口,如果12.17.56.44:7000映射IP为102.34.56.44 那么5123就是映射器7000端口
    这样
    12.17.56.44
    就被一个互联网IP22.18.55.33:7000 和内网12.17.56.44.:5213以及12.17.56.44:7000做了映射代理,就是这三个ip都能访问到web工程。

    版权声明:本文为博主原创文章,未经博主允许不得转载。

    以上就介绍了一个nignx 服务反向代理3个IP访问的配置方法,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    专题推荐:server index html quot location
    上一篇:nginx 简介篇 下一篇:自己动手写 PHP MVC 框架(40节精讲/巨细/新人进阶必看)

    相关文章推荐

    • 聊聊PHP escapeshellarg函数使用的中文问题• PHP原生类的总结分享• 分享PHP函数使用小工具(附代码示例)• PHP安全编码总结(经验分享)• 非常全面!PHP常见漏洞代码总结!
    1/1

    PHP中文网