Home >Backend Development >PHP Tutorial >Summary of Nginx reverse proxy settings

Summary of Nginx reverse proxy settings

WBOY
WBOYOriginal
2016-07-29 08:59:061384browse

There are many types of load balancing technologies. There are many commonly used layer four/seven load balancing technologies, and Nginx reverse proxy is one of them.

The following configuration is an example of Nginx reverse proxy configuration:

user  www www;
worker_processes  10;

error_log  /data1/logs/error.log crit;

pid        /usr/local/webserver/nginx/nginx.pid;

worker_rlimit_nofile 51200;

events {
	use epoll;
    worker_connections  51200;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
	
	#charset utf-8
	
	server_names_hash_buckets_size 128;
	client_header_buffer_size 32k;
	large_client_header_buffers 4 32k;
	
    #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;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;
	
	tcp_nodelay on;
	
	fastcgi_connect_timeout 300;
	fastcgi_send_timeout 300;
	fastcgi_read_timeout 300;
	fastcgi_buffer_size 64k;
	fastcgi_buffers 4 64k;
	fastcgi_busy_buffers_size 128k;
	fastcgi_temp_file_write_size 128k;

    gzip on;
	gzip_min_length 1k;
	gzip_buffers 4 16k;
	gzip_http_version 1.1;
	gzip_comp_level 2;
	gzip_types text/plain application/javascript text/css application/xml;
	gzip_vary on;
	
	client_max_body_size 300m;
	client_body_buffer_size 128k;
	
	proxy_connect_timeout 600;
	proxy_read_timeout 600;
	proxy_send_timeout 600;
	proxy_buffer_size 16k;
	proxy_buffers 4 32k;
	proxy_busy_buffers_size 64k;
	proxy_temp_file_write_size 64k;
	
	upstream php_server_pool {
	  server 192.168.1.10:80 weight=4 max_fails=2 fail_timeout=30s;
	  server 192.168.1.11:80 weight=4 max_fails=2 fail_timeout=30s;
	  server 192.168.1.12:80 weight=4 max_fails=2 fail_timeout=30s;
	}
	
	upstream message_server_pool {
	  server 192.168.1.13:3245;
	  server 192.168.1.14:3245 down;
	}
	
	upstream php_server_pool {
	  server 192.168.1.15:80 weight=1 max_fails=2 fail_timeout=30s;
	  server 192.168.1.16:80 weight=1 max_fails=2 fail_timeout=30s;
	  server 192.168.1.17:80 weight=1 max_fails=2 fail_timeout=30s;
	  server 192.168.1.18:80 weight=1 max_fails=2 fail_timeout=30s;
	}
	
    server {
        listen       80;
        server_name  www.yourdomain.com;

        #charset koi8-r;

        access_log  /data1/logs/www.yourdomain.com_access.log;
		
		location / {
			proxy_next_upstream http_502 http_504 error timeout invalid_header;
			proxy_pass http://php_server_pool;
			proxy_set_header Host www.yourdomain.com;
			proxy_set_header X-Forwarded-For $remote_addr;
        }

        # 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;
        #}
    }
	
	server {
		listen 80;
		server_name www1.yourdomain.com;
		
		localtion /message/ {
			proxy_pass http://message_server_pool;
			proxy_set_header Host $host;
		}
		
		localtion / {
			proxy_pass http://php_server_pool;
			proxy_set_header Host $host;
			proxy_set_header X-Forwarded-For $remote_addr;
		}
		
		access_log  /data1/logs/message.yourdomain.com_access.log;
	}

	server {
		listen 80;
		server_name bbs.yourdomain.com *.bbs.yourdomain.com;
		
		location / {
			proxy_pass http://bbs_server_pool;
			proxy_set_header Host $host;
			proxy_set_header X-Forwarded-For $remote_addr;
		}
		
		access_log off;
	}
	
    # 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 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}
In the above configuration, upstream is used to configure a group of proxy servers that can be used in proxy_pass or fastcgi_pass. The default load balancing method is round robin Inquire. server is used to specify the name and parameters of the server. The name can be a domain name, an IP address, a port number or a UNIX Socket.

proxy_pass and fastcgi_pass can set up a reverse proxy upstream cluster.

proxy_set_header is used to add specified Header information when sending a request to the reverse proxy server. The Host parameter is used to let the back-end server know which virtual host will handle it. X-Forwarded-For is used so that the back-end server can obtain the user's real IP through $_SERVER["HTTP_X_FORWARDED_FOR"].

The above introduces the summary of Nginx reverse proxy settings, including the relevant aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn