nginx basic tutorial (introduction to configuration file structure and simple example)


 Modules and Directives:

nginx consists of modules, which are controlled by directives specified in configuration files. Directives are divided into simple directives and block directives. A simple directive consists of a name and parameters, separated by spaces and terminated
by a semicolon (  ). A block directive has the same structure as a simple directive, but instead of ending with a semicolon, it ends with a set of additional directives surrounded by curly braces ( and ).;
{}

Basic block structure:

http {
    server {
    }
}

The following locations should be placed in the server block:

location / {
    root /data/www;
}

There can be multiple locations, if there are multiple matching locationblocks, nginx will choose the block with the longest prefix. The block above locationis a slash "/", which provides the shortest prefix with a length of 1, so location this block will only be used if all other blocks fail to provide a match.

Ok, next, add a location block to the server block, which will match requests starting with /images/ ( location /also matches such requests, but with a shorter prefix).

location /images/ {
    root /data;
}

The integrated configuration file structure is:

server {
    location / {
        root /data/www;
    }

    location /images/ {
        root /data;
    }
}

The above configuration file can accomplish the following meaning:
listen on standard port 80, in response to requests starting with URI /images/, the server will /data/imageslook for files from the directory. For example, in response to  http://localhost/images/example.pngrequests, nginx will look /data/images/example.pngfor files. If such a file does not exist, nginx will send a response indicating a 404 error.
Requests that do not start with a URI /images/will be mapped to a /data/wwwdirectory. For example, in response to  http://localhost/some/example.htmlrequests, nginx sends /data/www/some/example.htmlfiles.

nginx can also be used as a proxy, but static images use local files, for example:

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

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

nginx can also act as a proxy for Fastcgi, which can be used to route requests to FastCGI servers running applications built using various frameworks and programming languages ​​such as PHP. The most basic nginx configuration to use the FastCGI server consists of using the  fastcgi_pass  directive instead of the proxy_passdirective, and using the fastcgi_param  directive to set the parameters passed to the FastCGI server. Assuming that the FastCGI server can be localhost:9000based on the proxy configuration in the previous section, proxy_passreplace directives with directives fastcgi_passand change parameters to  localhost:9000. In PHP, SCRIPT_FILENAMEparameters are used to determine the script name and QUERY_STRING parameters are used to pass request parameters.

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

All requests except static image requests are routed to  localhost:9000a proxy server running through the FastCGI protocol.

Define an example of error handling for requests without host name:

server {
    listen      80;
    server_name "";
    return      444;
}

An example of a configuration file for a classic php website:

server {
    listen      80;
    server_name example.org www.example.org;
    root        /data/www;

    location / {
        index   index.html index.php;
    }

    location ~* \.(gif|jpg|png)$ {
        expires 30d;
    }

    location ~ \.php$ {
        fastcgi_pass  localhost:9000;
        fastcgi_param SCRIPT_FILENAME
                      $document_root$fastcgi_script_name;
        include       fastcgi_params;
    }
}

Post a Comment

Previous Post Next Post