The Nginx reverse proxy configuration (two)

  We talked earlier in the security chain Nginx reverse proxy and open nginx proxy caching, review Refer https://www.cnblogs.com/qiuhom-1874/p/12417130.html; Today we continue to say nginx reverse proxy, a major say on the next nginx reverse proxy using the http protocol, the proxy RUL with and without processing logic RUI; we can put this front and rear end are the same the agreement is understood as isomorphic agent, that front-end user access nginx http or https protocol proxy server to access back-end server is also really provide services based on the http protocol, the front and rear ends are using the same protocol, we say this It is isomorphic; Similarly, there have homogeneous isomers, isomers of different protocols is used in front and rear ends, the front end user such as http or https protocol, the proxy and the backend server using fastcgi protocol, which is called isomers; Internet the vast majority of sites are not pure static html pages, many sites all have their own application server, for example, users access the data we need is a script, or data after the execution of a program, then we'll need to perform later the script called server application server; nginx web server as though it has many functions, but he did not execute it These script capabilities such as php scripts, popular talk, nginx can handle only static text, you can not perform your own php script (need to rely on other application servers), and it is similar to httpd, httpd can also dynamically load php module, allowed to have execution ability php script, nginx is not, in order to achieve such a function, nginx will support a request he could not handle the back-end proxy client by fastcgi protocol resources; of course there are many such similar agreements, scgi, uwsgi and so on; like on this heterogeneous architecture, its principle is this, the client requests a resource script, such as, index.php file, if we do not put the user's request broker to the back-end application server, the client saw the data is in the index.php script content, which is obviously unreasonable; how to do it? Normally we need on nginx server configuration, user access certain.

  ngx_http_fastcgi_module: This module implements nginx allow the user to request the proxy server fastcgi

    1, fastcgi_pass address: address setting fastcgi server, this IP address can be added in the form of a port, or may be a path domain name or UNIX domain sockets; This option can be used if in the location and configuration section location

  Examples

fastcgi_pass localhost:9000

  Note: The above configuration means that by fastcgi protocol service response 9000 on behalf of the anti-machine interface;

fastcgi_pass Unix:/tmp/fastcgi.socket;

  Note: The above represents a set fastcgi address is the /tmp/fastcgi.socket document;

  2, fastcgi_index name; default home resources provided fastcgi name; This instruction can be used in the http, server, location configuration section

  3, fastcgi_param parameter value [if_not_empty]: setting a parameter for communication to the backend server fastcgi value of the parameter can be text, variables, or combinations of both.

  Examples

fastcgi_param SCRIPT_FILENAME /home/www/scripts/php$fastcgi_script_name;

  Note: The above configuration represents the SCRIPT_FILENAME equal to / home / www / scripts / php $ fastcgi_script_name passed to the fastcgi server, $ fastcgi_script_name expressed script name of the user request; mean to tell fastcgi server that put the script is executed back to the client (here The client usually refers to a proxy server)

fastcgi_param QUERY_STRING $query_string;

  Tip: We know a script pass different parameters, implementation of the results would be different; the above configuration represents the parameters of the request by the script variable QUERY_STRING saved and passed to the back-end server fastcgi, let it run a script, save the QUERY_STRING the value is passed as a parameter to the script; of course what we deliver to the back end a lot of inconvenience eleven written, is not likely to eleven written to the configuration file, there is a file in nginx configuration directory, specialized storage on nginx the variable corresponding to the variable back-end server, a profile fastcgi fastcgi.conf this configuration we typically include directive file we need to define nginx introduced into the variable to the server via fastcgi fastcgi variable;

  Examples

location ~* \.php$ {
	root           /usr/share/nginx/html;
	fastcgi_pass   127.0.0.1:9000;
	fastcgi_index  index.php;
	fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html$fastcgi_script_name;
	include        fastcgi_params;
}

  Note: The above configuration means that match the user URI, URI if the user ends in .php, it is processed (executed) by 127.0.0.1:9000 port fastcgi protocol proxy to the unit, the default home page fastcgi server resources called index.php and told fastcgi server to / usr / share / nginx find the resources requested by the user script / html / down, if there are other parameters and instructions to pass on the use of / etc / nginx / fastcgi_params in the definition of variable holds the nginx the value passed to the backend server fastcgi, the above is a configuration example of the anti-generation dynamic php page, with the above configuration can be used as a proxy server nginx responsiveness php dynamic scenario a client request;

[root@www nginx]# vim /etc/nginx/conf.d/proxy.conf 
  


server {

        server_name www.test.com;

        proxy_cache proxy_cache;
#       proxy_cache_key $request_uri;
#       proxy_cache_methods GET HEAD;
        proxy_cache_valid 200 302 15m;
        proxy_cache_valid 404 1m;
#       proxy_cache_use_stale http_500 http_502;
        location  /en/docs/ {
                proxy_pass http://nginx.org;
        }
        location ~* /(.*).php$ {
                root /app/php_scripts;
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME /app/php_scripts$fastcgi_script_name;
                include /etc/nginx/fastcgi_params;
        }
}
~                                                                                                                                                                          
~                                                                                                                                                                          
~                                                                                                                                                                          
~                                                                                                                                                                          
~                                                                                                                                                                          
~                                                                                                                                                                          
~                                                                                                                                                                          
~                                                                                                                                                                          
~                                                                                                                                                                          
~                                                                                                                                                                          
~                                                                                                                                                                          
~                                                                                                                                                                          
~                                                                                                                                                                          
~                                                                                                                                                                          
~                                                                                                                                                                          
~                                                                                                                                                                          
~                                                                                                                                                                          
~                                                                                                                                                                          
~                                                                                                                                                                          
~                                                                                                                                                                          
~                                                                                                                                                                          
~                                                                                                                                                                          
~                                                                                                                                                                          
"conf.d/proxy.conf" 23L, 500C written
[root@www nginx]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@www nginx]# mkdir /app/php_scripts -p
[root@www nginx]# cat >> /app/php_scripts/test.php 
<?php
        phpinfo();
?>
^C
[root@www nginx]# cat /app/php_scripts/test.php 
<?php
        phpinfo();
?>
[root@www nginx]# nginx -s reload
[root@www nginx]# 

  Tip: Before configuring the above need to install php-fpm, after installation you need to change /etc/php-fpm.d/www.conf in the user and group, usually depends on the front-end proxy that is user-initiated, we started with the back-end user can, we front-end agents nginx, nginx can here need to change into after such changes, save the configuration, and then restart php-fpm, then you can see in this machine 127.0.0.1: 9000 is in a listening state; above configuration means that the user requests ending in .php resources to anti behalf 127.0.01: 9000 for processing; and clearly illustrates the back-end fastcgi Home resource name is index.php, the back-end application server root path / app / php_scripts; fastcgi_param SCRIPT_FILENAME / app / php_scripts $ fastcgi_script_name role with nginx in the root alias command is the same, URI specified user to access the corresponding path on the disk, the equivalent of a backend server working directory fastcgi it;

  Tip: You can see that we are able to access test.php be executed php-fpm server and returns the result of execution;

  4, 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]; this instruction is the same proxy_cache_path as proxy cache definition of roles fastcgi; wherein levels represents level defines the cache directory, up to 3 levels, each level a maximum of two characters; keys_zone = name: size represents k / name and the size of the memory space v mapping; inactive = time said in an inactive long; max_size = size representation on the specified disk buffer space limit for the cache storage path;

  5, fastcgi_cache zone | off; call the specified cache space to cache data; can be used in http, server, location configuration section

  6, fastcgi_cache_key string; is defined as a string of key cache entry;

  7, fastcgi_cache_methods GET | HEAD | POST ......; those defined request method using the cache; the default is GET and HEAD

  8, fastcgi_cache_min_uses number; the number of times specified here is defined buffer space in the cache entry inactivity time inactive at least to be defined before access to the cache entry is considered active, not be cleaned up;

  9, fastcgi_cache_valid [code] tiem; different setting of each response code buffer length;

  10, fastcgi_keep_conn on | off ;: set whether to maintain the connection, by default, FastCGI server will close the connection immediately after sending the response. However, when this command is set to a value on, nginx indicating FastCGI server keep the connection open.

  Examples

   Tip: When you define a buffer space must be defined in the http configuration section, above represent buffer space to define a fastcgi, and its name is php_cache, using 10m space in memory, the path on the physical disk is / cache / proxy / under php-fpm / directory, a maximum space 2g;

   Note: The above configuration represents the buffer space to use fastcgi of php_cache, cache key for the $ request_uri, is to a user request uri hash as a cache routing, what this means is that nginx lookup cache is to find the cache by hash what the above configuration is defined nginx the user request URI as the hash subject; method requested by the user using is GET or HEAD method using a cache; cache minimum number of hits is 3, is to say in the inactive duration, the cache entry hit count is less than 3 will be considered that the cache is invalid, the validity of the next check cache will be cleared; response code resource cache 200 or 302 for 15 minutes, the response code resource cache 404 for 1 min; and opening long connection fastcgi backend server;

  Tip: You can see when we visit test.php page, in the buffer space defined in our generation caching, but we visited a test.php this page, why do three cache? In fact, we can open a web developer mode browser to view the situation it requests and responses

   Tip: You can see our test.php access this page, which is backed by three requests, so we see in the cache space has three cache entry, in fact, the cache space cache entry for each cache entry represents a cache resources, we can view the contents of the cache entry through the cat command; as follows

   提示:我们可以看到里面的缓存项纪录的信息,其中有一项KEY 就是我们指定的fastcgi_cache_key 说指定变量的值;我们查看缓存项里的内容很可能出现乱码,原因是缓存的内容有二进制,或者其他字符编码的内容;

  以上就是nginx作为反向代理服务器代理动态资源的介绍,总结一点,nginx代理动态资源和代理http资源的思想是类似的,只是使用的指令不一样;尤其对于缓存,两者的思想几乎完全一致,只是使用的模块和指令不同而已;对于动态资源我们除了要指定被代理的地址外,还需要指定被代理端资源路径,导入nginx变量与fastcgi变量对应的配置指令文件;对于缓存,两者没有什么区别;

Guess you like

Origin www.cnblogs.com/qiuhom-1874/p/12430543.html