[Linux] PHP programmers play with Linux series-nginx beginner guide, linux-nginx

Source: Internet
Author: User

[Linux] PHP programmers play with Linux series-nginx beginner guide, linux-nginx

1. PHP programmers turn to the Linux series-how to install and use CentOS

2. PHP programmers play with Linux series-lnmp Environment Construction

3. PHP programmers play with the Linux series-build an FTP code Development Environment

4. PHP programmers turn to the Linux series-back up and restore MySQL

5. PHP programmers turn to the Linux series-automatic backup and SVN

6. PHP programmers go to Linux-install nginx on Linux and Windows

 

Translated from official website

Nginx has a master process and many worker processes. the main purpose of the master process is to read and execute the configuration file to maintain the worker process. the worker process actually processes requests. nginx allocates worker processes based on the event model and operating system. The number of worker processes in the configuration file is generally configured as the number of CPU cores. the default configuration file name isnginx.conf, The directory address is generally in/usr/local/nginx/conf,/etc/nginx, Or/usr/local/etc/nginx

 

Start, close, and reload Configuration

nginxEnable

nginx -s reloadReload the configuration file

nginx -s quitExit elegantly

nginx -s reopenRe-open the log file

 

Static content service

Open the configuration file, which contains an example of server blocks.

http {    server {    }}

Default nginx configuration filenginx.confThe include command contains/etc/nginx/conf.d/The suffix in this directory is.confAll 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, Editdefault.confFile, usually there are many configuration files, each configuration file has a server block, nginx by their listening port and server_name to distinguish, it also compares the location command parameters in the request header with the server block.

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

This location block specifies"/"Prefix: Compare the requested URI. For the matched URI, This URI will be spliced to the end of the path specified by the root command. In other words, to form a path in the local file system, it is to request/var/www/html.

If there are many location commands, nginx will select the longest prefix. The above location block provides a shortest prefix. It will be used only when none of the other locations are matched.

Next, add the secondlocaltionBlock

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

When the request/images/Start, the second location will match (location / This request will also be matched, but its prefix is shorter than the second one)

Now it can work normally as the web service configuration file, listening to port 80. Enterhttp://localhostYou can access the service. When the request URI is/images/The server will respond/data/imagesDirectory. For example, when the request ishttp://localhost/images/example.png, Nginx will respond/data/images/example.pngFile. If the file does not exist, nginx will respond to the 404 error.

When the requested URI is not/images/The request is mapped/var/www/htmlDirectory. For example, the requested URI ishttp://localhost/some/example.html, Nginx will respond/var/www/html/some/example.htmlFile.

Apply the new configuration, enable nginx or send a reload signal to the master process of nginx, and execute the following command:

nginx -s reload

In case of unexpected errors, goaccess.logAnderror.logFind the reason, the Directory of the two files is in/usr/local/nginx/logsOr/var/log/nginx

 

Simple reverse proxy service configuration

The most common function of nginx is to act as a reverse proxy server, which means that this service needs to receive requests, direct the requests to the service to be proxy, retrieve the response from it, and send the response to the client.

We will configure the basic reverse proxy server. This service processes requests for image files from the local directory and sends all other requests to the proxy server. in this example, both services are defined in an nginx instance.

First, define a proxy server and add a new one in the nginx configuration file.serverBlock, as follows:

server {    listen 8080;    root /data/up1;    location / {    }}

This simple service listens to port 8080 (we have never used the listen command before, because it is the listening port 80 by default) and maps all requests to the local file system./data/up1Directory. Create this directory and put it inindex.htmlFile. Note that the root command is placedserverContext. WhenlocationThe root command will be used if there is no root command under the Block.

Next, use the service in the previous step to modify the configuration as a reverse proxy server.locationAddproxy_passCommand to specify the protocol name and proxy service port in the parameter (in this example, It is 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 suffixes.gif.jpgOr.pngTo use a regular expression.. The request will be mapped/data/imagesDirectory

 

Configure FastCGI proxy service

Nginx can be used to route requests to the FastCGI service, so that you can run various frameworks and PHP applications.

The most basic nginx configuration, usefastcgi_passCommand replacementproxy_passCommand,fastcgi_paramCommand to set the parameters passed to the FastCGI server. Assume that the FastCGI server runs inlocalhost:9000. Take the reverse proxy configuration example in the previous section and change the parameterlocalhost:9000. In PHP,SCRIPT_FILENAMEThe parameter is used to determine the Script Name,QUERY_STRINGUsed to pass request parameters. The configuration is 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;    }}

Now a service is created, which routes all requests outside of the static image file to the server to which the proxy is sent. The proxy server runs inlocalhost:9000.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.