Nginx anti-generation related

ngx_http_proxy_module

Nginx also provides the function of anti-generation server, the same as lvs, except that lvs is a forwarding server that forwards all requests to the back-end server, and nginx replaces the client to access the back-end server.

At the same time, the anti-generation of nginx also has the function of ldirector, which detects the heartbeat of the back-end server and automatically shuts down the back-end hosts that cannot be served.

proxy_pass URL

Send the request to the back-end server.

[root@www conf.d]# cat a.com.conf 
server{
    
    
	listen 80 default_server;
	server_name www.a.com;
	root /data/a;
	location / {
    
    
		proxy_pass http://192.168.192.137:80;
	}


}
#将所有的请求都发送到137主机上

[root@localhost ~]# curl http://www.a.com
192.168.192.137

When uri is defined, if the uri at the end of proxy_pass is not included /, the directory defined by location will be appended /.
Example:

[root@www conf.d]# cat a.com.conf 
server{
    
    
	listen 80 default_server;
	server_name www.a.com;
	root /data/a;
	location / {
    
    
		proxy_pass http://192.168.192.137:80;
	}
	location /images {
    
       
		proxy_pass http://192.168.192.137:80;  #此处最后没有添加/,那么location中的/images会自动附加到192.168.192.137:80/images。所以访问images下时会自动跳转到137的默认页面
	}


}

Insert picture description here

If proxy_pass is added last /, then the uri of proxy_pass directly replaces the content in location. It is equivalent to directly access the uri under proxy_pass when accessing the back-end server.

[root@www conf.d]# cat a.com.conf 
server{
    
    
	listen 80 default_server;
	server_name www.a.com;
	root /data/a;
	index index.html index.php;
	location / {
    
    
		proxy_pass http://192.168.192.137:80;
	}
	location /images {
    
    
		proxy_pass http://192.168.192.137:80/picture/;
	}


}

Insert picture description here

If the location uses regular expressions, then proxy_pass must not be followed by uri. If there is a uri after proxy_pass, an error will be reported. The first mode must be used, without it /, adding the uri in the location to the end of the proxy_pass.

proxy_set_header field value

Set the value of the request header of the request message sent to the back-end server.
By default, the client ip in the request message received by the back-end server is the ip address of the nginx server, but it will appear that the real access cannot be counted. Ip address of the client. This option can customize the header value

[root@www conf.d]# cat a.com.conf 
server{
    
    
	listen 80 default_server;
	server_name www.a.com;
	root /data/a;
	index index.html index.php;
	proxy_set_header X-Real-IP $remote_addr;   #将客户端ip地址命名为X-Real-IP。
	location / {
    
    
		proxy_pass http://192.168.192.137:80;
	}
	location /images {
    
    
		proxy_pass http://192.168.192.137:80/picture/;
	}

}


apache主机上将logformat进行修改
  LogFormat "%{X-Real-IP}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined


重启服务之后再次访问
[root@localhost ~]# tail -f /var/log/httpd/access_log
192.168.192.134 - - [16/Apr/2020:21:18:58 +0800] "GET / HTTP/1.0" 200 16 "-" "curl/7.29.0"  # 134为nginx的私网地址。

192.168.199.132 - - [16/Apr/2020:21:20:52 +0800] "GET / HTTP/1.0" 304 - "-" "Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0"

Definition and cache related

proxy_cache_path

To set the cache path and other parameters, you can only define the
syntax in http :proxy_cache_path path [levels=levels] [use_temp_path=on|off] keys_zone=name:size [inactive=time] [max_size=size] [manager_files=number] [manager_sleep=time] [manager_threshold=time] [loader_files=number] [loader_sleep=time] [loader_threshold=time] [purger=on|off] [purger_files=number] [purger_sleep=time] [purger_threshold=time];

proxy_cache zone | off

The default is off, which indicates the cache to be called, or closes the cache mechanism;

proxy_cache_key string

The content of the key used in the cache

proxy_cache_valid [code …] time

Define the cache duration of the specific response code content defined in http

According to the above instructions, specify in the configuration file:

[root@www ~]# cat /etc/nginx/nginx.conf | grep proxy_*
    proxy_cache_path /var/cache/a_cache/proxy_cache   # 存放cache的目录
		     levels=1:1:1 keys_zone=proxy_cache:20m
		     存放目录级别    存放的名字以及空间大小
    proxy_cache proxy_cache;   启动哪个名字的缓存
    proxy_cache_key $request_uri;   针对哪个来定义缓存
    proxy_cache_valid 301 200 302 1h;  
    proxy_cache_valid any 1m;


[root@www ~]# ls /var/cache/a_cache/proxy_cache/9/d/7/6666cd76f96956469e7be39d750cc7d9 
/var/cache/a_cache/proxy_cache/9/d/7/6666cd76f96956469e7be39d750cc7d9
访问之后就会将缓存存放在这个目录中。

ngx_http_fastcgi_module

Forward the request to the fastcgi server, does not support the php module method

fastcgi_pass address

address is the address of the backend fastcgi server

fastcgi_index name

Fastcgi default homepage resources

fastcgi_param parameter value [if_not_empty]

Set the parameter value passed to the FastCGI server, which can be text, variable or combination.

Example:

[root@nginx conf.d]# cat a.com.conf
server{
    
    
	listen 80 default_server;
	server_name www.a.com;
	root /data/a;
	index index.html index.php;
	proxy_set_header X-Real-IP $remote_addr;

	location ~* \.php$ {
    
    
		fastcgi_pass 11.2.3.63:9000;
		fastcgi_index index.php;
		fastcgi_param SCRIPT_FILENAME /data/a$fastcgi_script_name;#设置此选项相当于访问/index.php时,访问的目录是/data/a/index.php
		include fastcgi_params; 
	}	

}


#在php-fpm主机上安装php-mysql模块和index.php
[root@php-fpm ~]# cat /data/a/index.php 
<?php
 $conn = mysqli_connect('11.2.2.228','wpuser','centos');
 if ($conn)
echo "OK";
else
echo "Failure";
 #echo mysql_error();
 mysql_close();
 ?>

Insert picture description here

fastcgi cache settings

fastcgi_cache_path path [levels=levels] [use_temp_path=on|off] keys_zone=name:size [inactive=time] [max_size=size] [manager_files=number] [manager_sleep=time] [manager_threshold=time] [loader_files=number] [loader_sleep=time] [loader_threshold=time] [purger=on|off] [purger_files=number] [purger_sleep=time] [purger_threshold=time];

There is no big difference from the previous proxy_pass cache, and the same can only be defined in http

fastcgi_cache zone | off

Call the specified cache space to cache data

fastcgi_cache_key string

Define a string used as a cache item

fastcgi_cache_methods GET | HEAD | POST …

For which request methods to use caching

fastcgi_cache_min_uses number

The cache item in the cache space must be accessed at least the number of times specified here during the inactive time defined by inactive before it can be considered as an active item

fastcgi_keep_conn on | off

After receiving the response from the back-end server, whether the fastcgi server closes the connection, it is recommended to enable the long connection.

fastcgi_cache_valid [code …] time

The cache duration of different response codes

Example:

 fastcgi_cache_path /var/cache/nginx/fcgi_cache
                       levels=1:2:1 keys_zone=fcgicache:20m 
		       inactive=120s max_size=1g;
    fastcgi_cache fcgicache;
    fastcgi_cache_key $request_uri;
    fastcgi_cache_valid 200 302 10m;
    fastcgi_cache_valid 301 1h;
    fastcgi_cache_valid any 1m;		

# 访问index.php之后,会将请求缓存在/var/cache/nginx下。
[root@nginx ~]# ls /var/cache/nginx/fcgi_cache/1/af/5/e251273eb74a8ee3f661a7af00915af1 
/var/cache/nginx/fcgi_cache/1/af/5/e251273eb74a8ee3f661a7af00915af1

Guess you like

Origin blog.csdn.net/qq_44564366/article/details/105564304