Description of main functions of Nginx

Keywords: Programming PHP SSL Nginx Unix

I. Providing Web Services

Nginx itself is a server of static resources, and other dynamic resources are handed over to other programs by matching.

server {
	listen 80 default_server;
	listen [::]:80 default_server;

	# SSL configuration
	#
	# listen 443 ssl default_server;
	# listen [::]:443 ssl default_server;
	#
	# Note: You should disable gzip for SSL traffic.
	# See: https://bugs.debian.org/773332
	#
	# Read up on ssl_ciphers to ensure a secure configuration.
	# See: https://bugs.debian.org/765782
	#
	# Self signed certs generated by the ssl-cert package
	# Don't use them in a production server!
	#
	# include snippets/snakeoil.conf;

	root /var/www/html;

	# Add index.php to the list if you are using PHP
	index index.html index.htm index.nginx-debian.html;

	server_name _;

	location / {
		# First attempt to serve request as file, then
		# as directory, then fall back to displaying a 404.
		try_files $uri $uri/ =404;
	}

	# pass PHP scripts to FastCGI server
	#
	#location ~ \.php$ {
	#	include snippets/fastcgi-php.conf;
	#
	#	# With php-fpm (or other unix sockets):
	#	fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
	#	# With php-cgi (or other tcp sockets):
	#	fastcgi_pass 127.0.0.1:9000;
	#}

	# deny access to .htaccess files, if Apache's document root
	# concurs with nginx's one
	#
	#location ~ /\.ht {
	#	deny all;
	#}
}

2. Forward Agency

Proxy Client Access Server

resolver 114.114.114.114 8.8.8.8;
  server {

      resolver_timeout 5s;

      listen 81;

      access_log  e:\wwwroot\proxy.access.log;
      error_log  e:\wwwroot\proxy.error.log;

      location / {
          proxy_pass http://$host$request_uri;
      }
  } 

III. Reverse Agency

Requests sent by clients are forwarded to source servers via proxy servers

server {  
      listen      80;                                                        
      server_name  localhost;                                              
      client_max_body_size 1024M;

      location / {
          proxy_pass http://localhost:8080;
          proxy_set_header Host $host:$server_port;
      }
  } 

4. Reverse Proxy Load Balancing

Three Load Balancing Strategies

1.round-robin polling (default)

Requests to the application server are circulated

upstream test {
      server localhost:8080;
      server localhost:8081;
  }
  server {
      listen      81;                                                        
      server_name  localhost;                                              
      client_max_body_size 1024M;

      location / {
          proxy_pass http://test;
          proxy_set_header Host $host:$server_port;
      }
  } 

### 2.least-connected least connection The next request is assigned to the server with the fewest active connections

upstream test {
      least_conn;
      server localhost:8080;
      server localhost:8081;
  }
  server {
      listen      81;                                                        
      server_name  localhost;                                              
      client_max_body_size 1024M;

      location / {
          proxy_pass http://test;
          proxy_set_header Host $host:$server_port;
      }
  } 

### 3. ip-hash (shared session) The same server is assigned according to the ip address. That is, when the same ip is accessed again, the previous server is accessed directly to use session.

upstream test {
      ip_hash; 
      server localhost:8080;
      server localhost:8081;
  }
  server {
      listen      81;                                                        
      server_name  localhost;                                              
      client_max_body_size 1024M;

      location / {
          proxy_pass http://test;
          proxy_set_header Host $host:$server_port;
      }
  } 

4. Custom Weight Assignment

If the weight is high, the probability of allocation will be higher.

upstream test {
      server localhost:8080 weight = 3;;
      server localhost:8081;
  }
  server {
      listen      81;                                                        
      server_name  localhost;                                              
      client_max_body_size 1024M;

      location / {
          proxy_pass http://test;
          proxy_set_header Host $host:$server_port;
      }
  } 

Posted by binarylime on Mon, 30 Sep 2019 01:35:25 -0700