nginx之反代相关

ngx_http_proxy_module

nginx也提供反代服务器的功能,跟lvs一样,只不过lvs是转发服务器,将所有的请求的转发到后端服务器上,而nginx是代替客户端去访问后端服务器。

同时nginx的反代还具有ldirector的功能,检测后端服务器心跳,并自动down掉无法服务的后端主机。

proxy_pass URL

将请求发送到后端的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

当定义uri时,如果proxy_pass最后的uri不带/的话,那么location定义的目录将会附加到/上。
示例:

[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的默认页面
	}


}

在这里插入图片描述

如果proxy_pass最后添加/的话,那么proxy_pass的uri直接顶替location中的内容。相当于访问后端服务器的时候,直接访问proxy_pass下的uri。

[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/;
	}


}

在这里插入图片描述

如果location使用正则表达式,那么proxy_pass后面一定不能跟uri。如果proxy_pass后面有uri,将会报错。必须使用第一种模式,不带/,就是将location中的uri添加到proxy_pass最后边。

proxy_set_header field value

设定发往后端服务器的请求报文的请求首部的值
默认情况下,后端服务器接收到的请求报文中的客户端ip是nginx服务器的ip地址,但是这样就会出现无法统计真正访问的客户端ip地址。这一选项就可以自定义头部值

[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"

定义与缓存相关

proxy_cache_path

设置缓存的路径和其他参数,只能定义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

默认off,指明调用的缓存,或关闭缓存机制;

proxy_cache_key string

缓存中使用键的内容

proxy_cache_valid [code …] time

定义对特定响应码内容的缓存时长 定义在http中

根据上面指令,在配置文件中指定:

[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

转发请求到fastcgi服务器上,不支持php模块方式

fastcgi_pass address

address为后端的fastcgi server的地址

fastcgi_index name

fastcgi默认的主页资源

fastcgi_param parameter value [if_not_empty]

设置传递给 FastCGI服务器的参数值,可以是文本,变量或组合。

示例:

[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();
 ?>

在这里插入图片描述

fastcgi缓存设置

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];

同之前的proxy_pass的缓存没有什么大的区别,同样也只能定义在http中

fastcgi_cache zone | off

调用指定的缓存空间来缓存数据

fastcgi_cache_key string

定义用作缓存项的字符串

fastcgi_cache_methods GET | HEAD | POST …

为哪些请求方法使用缓存

fastcgi_cache_min_uses number

缓存空间中的缓存项在inactive定义的非活动时间内至少要被访问到此处所指定的次数方可被认作活动项

fastcgi_keep_conn on | off

收到后端服务器响应后,fastcgi服务器是否关闭连接,建议启用长连接。

fastcgi_cache_valid [code …] time

不同的响应码各自的缓存时长

示例:

 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

猜你喜欢

转载自blog.csdn.net/qq_44564366/article/details/105564304
今日推荐