PHP programmers play with the Linux series nginx beginner guide

  • 2020-05-30 21:46:03
  • OfStack

PHP programmers play with Linux series:

1.PHP programmers play with Linux series - how to install and use CentOS

2.PHP programmers play with the Linux series - building the lnmp environment

3.PHP programmers play with the Linux series - building the FTP code development environment

4.PHP programmers play with the Linux series - backup restore MySQL

5.PHP programmers play with the Linux series - automatic backup with SVN

6.PHP programmers play with the Linux series -Linux and Windows install nginx

Translated from official website documentation

nginx have 1 and a number of worker master process. The main purpose of master process is read and execute the configuration file, maintain worker process. worker process really handle the request. nginx to allocate worker process based on the event model and operating system, in the configuration file worker process number configured to CPU kernel number 1. The default configuration file name is nginx conf, in like directory address 1 / usr local/nginx/conf, / etc/nginx, or/usr local/etc/nginx

Start, close, and overload the configuration

nginx open

nginx-s reload reload the configuration file

nginx-s quit gracefully exits

nginx-s reopen re-open the log file

Static content service

Open the configuration file and it already contains an example of an server block


http {
 server {
 }
}

Default nginx configuration file nginx conf inside, use include instruction contains/etc/nginx/conf d/the suffix for directory. conf all configuration files


http {
 include /etc/nginx/mime.types;
 default_type application/octet-stream;

 log_format main '$remote_addr - $remote_user [$time_local] "$request" '
   '$status $body_bytes_sent "$http_referer" '
   '"$http_user_agent" "$http_x_forwarded_for"';

 access_log /var/log/nginx/access.log main;

 sendfile on;
 #tcp_nopush on;

 keepalive_timeout 65;

 #gzip on;

 include /etc/nginx/conf.d/*.conf;
}

In/etc nginx/conf d/directory and edit default. conf file, usually there will be a lot of configuration files, each in the configuration file has a server block, nginx through their listening ports and server_name will distinguish, it will head for the request and server block location command parameter comparison.


server {
 location / {
 root /var/www/html;
 }
}

The location block specifies the "/" prefix, more request URI, for matching to URI, the URI will be stitching at the back of the root instructions specified path. In other words, to constitute a path in the local file system, is to request/var/www/html.

If there are a lot of location instructions,nginx will choose the longest prefix. The location block above provides a shortest prefix that will only be used if all other location do not match.

In the next step, add the second localtion block


server {
 location / {
 root /var/www/html;
 }
 location /images/ {
 root /data;
 }
}

When the request starts with /images/, the second location will match to (location/will also match the request, but its prefix is shorter than the second)

Now it can be used as web service configuration file is normal work, listen on port 80. The local machine input http: / / localhost can access to the service. When the request URI begin with/images /, the server will respond/data images files in the directory. For example: when the request is http: / / localhost images/example png, nginx will respond/data/images/ES The 156en.png file. If the file does not exist,nginx will respond to a 404 error.

When URI don't begin with/images/request, the request will be mapped to/var/www/html directory. For example: request is URI http: / / localhost some/example html, nginx will respond/var www html/some/example html file.

Using the new configuration, start nginx or send reload signal to nginx master process, and execute the following command:

nginx -s reload

Than 1 has unpredictable errors to access. log and error log to find the reason, in these two files in the directory in/usr local/nginx/logs or/var log/nginx

Configure a simple reverse proxy service

The most common function of nginx is to act as a reverse proxy server, which means that the service receives the request, directs the request to the proxy service, retrieves the response from there, and sends it to the client.

We will configure the basic reverse proxy server that handles requests for image files from the local directory and sends all other requests to the proxy server. In this example, both services will be defined in an nginx instance.

First, define a proxy server and add a new server block to the nginx configuration file, as follows:


server {
 listen 8080;
 root /data/up1;

 location / {
 }
}

This simple service listening on port 8080 (before, we didn't use listen instructions, because is to monitor the port 80 by default), mapping all the request to the local file system/data/up1 directory. To create this directory, inside put index. html file. Note that root instructions on server context. When there is no root location block under the instruction, the root instruction will be used.

In the first location block, add an proxy_pass directive that specifies the protocol name and the port of the proxy service in the parameters (in this case, http://localhost:8080).


server {
 location / {
  proxy_pass http://localhost:8080;
 }

 location ~ \.(gif|jpg|png)$ {
  root /data/images;
 }
}

In the second location block, the parameter is a regular expression that matches all requests with the suffix.gif.jpg or.png. To use the regular expression, the corresponding request will be mapped to /data/images

Configure the FastCGI proxy service

nginx can be used as a routing request to the FastCGI service, which allows you to run a variety of framework and PHP applications

The most basic nginx configuration USES the fastcgi_pass directive to replace the proxy_pass directive,fastcgi_param directive to set the parameters passed to the FastCGI server. Assume that the FastCGI server is running on localhost:9000 S268EN is used as the pass request parameter and is configured as follows:


server {
 location / {
 fastcgi_pass localhost:9000;
 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
 fastcgi_param QUERY_STRING $query_string;
 }

 location ~ \.(gif|jpg|png)$ {
 root /data/images;
 }
}

A service is now created that routes all requests out of the static image file to the proxy server, which runs on localhost:9000 via the FastCGI protocol.


Related articles: