Nginx User Guide
1. Run nginx
You can run the nginx command to enable nginx:
nginx
If nginx is enabled, run the nginx command and add the-s parameter to control nginx running.
nginx -s signal
Signal Value:
stop
-Close quickly
quit
-Elegant Shutdown
reload
-Reload the configuration file.
reopen
-Reopen the log file.
For example, you can use the following command to disable nginx after nginx finishes processing the current request.
nginx -s quit
After modifying the configuration file, run the following command:
nginx -s reload
2. Simple nginx Configuration
Open the configuration file, usually in/etc/nginx. cnf, depending on your own installation parameters.
Nginx. conf already contains a server block configuration case, but it is commented out. Below is the basic configuration of a server Block
http { server { }}
Server block, you can configure some locations to specify the local resources corresponding to the request url.
location / { root /data/www;}
The above indicates that all access resources under/are under the/data/www folder.
location /images/ { root /data;}
This indicates that all images accessed by the/images/path are under/data.
The preceding unified configuration is
server { listen 8080;
location / { root /data/www; }
location /images/ { root /data; }
}
If I accesshttp://localhost/images/example.png
Then, nginx returns the example.png image under/data/images/in the file directory to the client.
If I accesshttp://localhost/some/example.htm
l
Nginx returns the example.html image under/data/www/in the file directory to the client.
Listen can be left unspecified. The default value is 8080.
If the configuration is modified during running
nginx -s reload
If the configuration is verified, but the specified file is not accessed as agreed, you can view/usr/local/nginx/logs
Or/var/log/nginx
The following log filesaccess.log
Anderror.log
3. Configure reverse proxy
server { location / { proxy_pass http://localhost:8080; } location /images/ { root /data; }}
Proxy_pass specifies the reverse proxy path. All the paths that match the/will go to http: // localhost: 8080 to obtain resources.
For example:http://192.168.1.100/some/example.htm
L The accessed resources are actuallyhttp://localhost/some/example.htm
l
Resources obtained, which are transparent to the client.
4. Host Name
The server name is specified through the server_name command, which determines which server to process which request. server_name can be specified through wildcards and regular expressions.
server { listen 80; server_name example.org www.example.org; ...}server { listen 80; server_name *.example.org; ...}server { listen 80; server_name mail.*; ...}server { listen 80; server_name ~^(?<user>.+)\.example\.net$; ...}
When a request meets multiple host names at the same time, the priority of the host names is as follows.
1. Full name, accurate name.
2. The longest wildcard name starting with "*"*.example.org
"
3. The longest wildcard name ending with * is"mail.*
"
4. the first virtual host name that matches the Regular Expression
Wildcards can only be used at the beginning and end of the host name.www.*.example.org
Andw*.example.org
"Are all incorrect statements. If you need to match this pattern, you can specify it using a regular expression, such as"~^www\..+\.example\.org$
"And"~^w.*\.example\.org$
". Part of the asterisk proxy host name"*.example.org
Not only www.example.com, but also www.sub.example.com..example.org
It can represent example.org or * .example.org.
To use a regular expression, the host name must be a Tilde ~ Start
server_name ~^www\d+\.example\.net$;
If it is not a Tilde ~ Or, it is considered as a full host name.
If the regular expression host name contains *, it is considered as a wildcard host name. ^ And $ are required. They are syntactic and logical requirements.
You can use regular expression capture to reference the following variables.
server { server_name ~^(www\.)?(?<domain>.+)$; location / { root /sites/$domain; }}
Regular Expression capturing supports the following syntax:
?<name > |
Perl 5.10 compatible syntax, supported since PCRE-7.0 |
?'name ' |
Perl 5.10 compatible syntax, supported since PCRE-7.0 |
?P<name > |
Python compatible syntaxes, supported since PCRE-4.0 |
Regular Expression capturing can also be obtained through numerical parameters.
server { server_name ~^(www\.)?(.+)$; location / { root /sites/$2; }}
$2 matches the content matched by the regular expression in the second bracket.
Hybrid Host Name
server { listen 80; server_name example.org www.example.org ""; ...}
If nginx does not have a server module that matches the requested url host name, an empty Host Name is returned by default to respond to the request.
If you access the server through an ip address, you can configure the ip host name to respond to the request.
server { listen 80; server_name example.org www.example.org "" 192.168.1.1 ; ...}
-The host name represents all the wrong host names.
server { listen 80 default_server; server_name _; return 444;}
In some cases, you may access * .example.com, but pack www.example.com and example.com, which are frequently accessed.
server { listen 80; server_name example.org www.example.org *.example.org; ...}
Not like this.
server { listen 80; server_name .example.org; ...}
If the host name is too long, you must modify the parameters in the http module.
server_names_hash_bucket_size
The value of this parameter can be 32 or 64, depending on the size of your cpu cache Stack
If you set it to 32, but your server name is long, for example:Too.long.server.name.example.org
could not build the server_names_hash,you should increase server_names_hash_bucket_size: 32
You must set its parameters to double
http { server_names_hash_bucket_size 64; ...
This error is reported if too many host names are configured.
could not build the server_names_hash,you should increase either server_names_hash_max_size: 512or server_names_hash_bucket_size: 32
The solution is to set server_names_hash_max_size as much as possible as the number of host names. If this configuration cannot be used up, or the nginx startup time is too long after the configuration, the value of server_names_hash_bucket_size will be increased.
Welcome to nginx!
Complete system solutions
How does php get the header using nginx?
The Nginx http module encapsulates environment variables differently from Apache when processing HTTP requests. In addition to some common variables related to the HTTP protocol, it also supports a series of Nginx self-contained variables, such as $ server_protocol and $ nginx_version in the fastcgi_params.default file under the Nginx configuration directory. As used in the example in this file, these variables can be passed to the cgi program when fastcgi is configured, so that they can be used as the environment variables of the cgi program. However, even with these self-contained variables, Nginx cannot fully meet all requirements.
If you know Jquery, you will find that Jquery uses setRequestHeader ('x-Requested-with', 'xmlhttprequest ') to implement Ajax ') the method automatically adds an X-Requested-With request header With the value "xmlhttprequest" to identify this Ajax request, in this case, the backend that processes the request can identify the request type by judging the identity. In this case, how does PHP obtain the value of this custom parameter?
People familiar with Apache and PHP will think of $ _ SERVER ["HTTP_X_REQUESTED_WITH"] for the first time. Good, this is a perfect solution for prime matches, but Nginx is not, this is determined by Nginx's positioning of its own work-Nginx is only responsible for HTTP. In the eyes of Nginx, PHP is just a backend. In terms of image, it only distributes requests, regardless of who it sends them. This means that we cannot expect Nginx to automatically pass some custom parameters to PHP Like Apache, and only have self-reliance. Simply put, if you want to directly call the value of the custom request header parameter like $ _ SERVER ["HTTP_X_REQUESTED_WITH"], you must manually add it to the fastcgi_params configuration, explicitly inform the cgi program to receive the message, otherwise Nginx will discard it.
For how to configure environment variables, refer to the fastcgi_params.default file, which is also mentioned in the previous blog "how to configure environment variables for virtual hosts in Nginx. For the above example, you only need to add a line to the fastcgi_params file:
? 12 # for Ajax fastcgi_param HTTP_X_REQUESTED_WITH $ http_x_requested_with;
In this way, After reloading Nginx configuration, you can call $ _ SERVER ["HTTP_X_REQUESTED_WITH"] in PHP to determine the request type. Pay attention to the following two points:
1. Names of custom request headers should not contain white spaces, colons, line breaks, or underscores.
When Nginx processes the header of the client request, it replaces the hyphen (-) in the name with the underscore "_", add "$ http _" to the lower case of all letters as the variable name corresponding to the name. For example, in the preceding Jquery example, setRequestHeader ('x-Requested-with', 'xmlhttprequest ') is a line of string in the HTTP request header: "X-Requested-With: xmlhttprequest ", after Nginx processing, a variable named $ http_x_requested_with is automatically generated and its value is "xmlhttprequest ". Note that the rule "-" replaces with underscore "_" indicates that Nginx cannot correctly recognize the request parameter name if it contains an underscore.
Ii. $ _ SERVER ["HTTP_X_REQUESTED_WITH"] ...... the remaining full text>