Introduction to Nginx

Source: Internet
Author: User
Tags sendfile

Installation
# tar zxvf nginx-1.10.0.tar.gz# cd nginx-1.10.0/#./configure--prefix=/usr/local/nginx-1.10.0 \               --user=nobody                      --group=nobody                     --without-select_module            --without-poll_module              


Note:
    • --without-select_module--without-poll_module: Because there is more efficient epoll available under Linux, select and poll are disabled.
    • --user=nobody--group=nobody: A non-privileged user. The worker process will use this user.

Start, stop, and reload
#/usr/local/nginx-1.10.0/sbin/nginx# Ps-ef | grep nginxroot       8195      1  0 10:33?        00:00:00 nginx:master process/usr/local/nginx-1.10.0/sbin/nginxroot       8196   8195  0 10:33?        00:00:00 nginx:worker process#/usr/local/nginx-1.10.0/sbin/nginx-s stop    //fast shutdown#/usr/local/ Nginx-1.10.0/sbin/nginx            #/usr/local/nginx-1.10.0/sbin/nginx-s quit    //graceful shutdown#/usr/local/ nginx-1.10.0/sbin/nginx#/usr/local/nginx-1.10.0/sbin/nginx-s reload//reloading the configuration file


Configuration file StructureNginx contains modules in which the behavior of the modules is controlled by the instructions (Directive) in the configuration file (usually nginx.conf), and the instructions are divided into simple commands (simpledirective) and block instructions (blockdirective).
Simpledirective: = Name [space-separated-param-list];
Blockdirective: = Name [Space-separated-param-list] {directive-list}
Directive-list: = Null | Simpledirective Directive-list | Blockdirective directive-list
If a blockdirective has an internal directive, then it is the context of the internal directive; In the configuration file, the outermost directive context is not visible in the main context. For example:
Worker_processes  1;events {    worker_connections  1024;} HTTP {    include       mime.types;    Default_type  Application/octet-stream;    Sendfile on      ;    Keepalive_timeout  ;    server {        listen       ;        server_name  localhost;        Location/{            root   html;            Index  index.html index.htm;}}}    


worker_processes, worker_connections, include, Default_type, Sendfile, Keepalive_timeout, listen, Server_ Name, root, index is simpledirective;
Events, HTTP, server, location is blockdirective;
The context of location is Server,server's context is the context of http,http and events is main;

Configuration Example 1: Service static contentAs an HTTP server, the most basic function of NIGNX is to service static content (HTML pages, pictures). In this example, we set up a server that returns an HTML page (located on the local file system/data/www) or a picture (located on the local filesystem/data/images), based on the user's request.
To do this, we need to configure a blockdirective server, which will protect two blockdirective location: one provides HTML page service and the other provides image service. The longest match for the URI requested by the user is the page location, the HTML page is returned, and if the image location, the picture is returned.

Create a local directory that corresponds to two location

<span style= "FONT-SIZE:14PX;" >mkdir-p/data/wwwmkdir-p/data/images</span>


Sample HTML pages and pictures

# vim/data/www/index.html<! DOCTYPE html>


Configuration file

# cp/usr/local/nginx-1.10.0/conf/nginx.conf/usr/local/nginx-1.10.0/conf/nginx.conf.bak# vim/usr/local/ Nginx-1.10.0/conf/nginx.confuser  nobody;worker_processes  4;events {    worker_connections  1024;} HTTP {    include       mime.types;    Default_type  Application/octet-stream;    Sendfile on        ;    Keepalive_timeout  ;    server {        listen       ;        server_name  localhost;        Location/{           root/data/www;        }        location/images/{           root/data;}}}    


Test

Reload Nginx

Visit http://192.168.75.138/index.htmlVisit Http://192.168.75.138/images/basketball.jpg

Note:
    • Obviously, in nginx.conf, we configured a server that listens on port 80 and server name is localhost. Interesting is the two location, which locates the static files requested by the user in the longest matching way. For example:
    • When accessing http://192.168.75.138/index.html, the URI is "/index.html". It matches the location/match, which is "/" and the match length is 1; it does not match the localtion/images/. So location/To locate the file requested by the user.
    • When accessing Http://192.168.75.138/images/basketball.jpg, the URI is "/images/basketball.jpg". It matches the location/match, the match is "/", the match length is 1; it matches the localtion/images/, the match is "/images/" and the match length is 8, which is the longest match. So the location/images/to locate the file requested by the user.
    • How do I locate a request file? The root + URI of the longest matching location. For the first, "/data/www" + "/index.html", which is the local file/data/www/index.html; for the second, "/data" + "/images/basketball.jpg", which is the local file/data/ Images/basketball.jpg.
Configuration Example 2: A simple proxy serverIn this example, we will configure two HTTP servers, one server as the other proxy. For simplicity, the two servers are running within the same Nginx instance, except for the ports. In fact, these two servers can run entirely within different nginx of different hosts.
    • Proxy server: Listens on port 80, and if the user requests a picture (URI matching "/images/"), it will provide the service from the local file system itself. For other requests, forward to the "proxy server";
    • Agent server (proxied server): Listens on port 8080. It also uses the local file system (/DATA/UPSTREAM1) to provide static services. In fact, this proxy server can be a different server, such as a dynamic page server tomcat.

Local file structure

# tree/data/
/data/
├──images
│└──basketball.jpg
└──upstream1
└──index.html

Configuration file


Worker_processes  4;events {    worker_connections  1024;} HTTP {    include       mime.types;    Default_type  Application/octet-stream;    Sendfile on        ;    Keepalive_timeout  ;    server {        listen       8080;        server_name  localhost-proxied;        root/data/upstream1;        Location/{        }    }    server {        listen       ;        server_name  localhost-proxy;        Location/{            proxy_pass http://localhost:8080/;        }        Location ~\. (gif|jpg|png) $ {            root/data/images;}}    }


Test

Reload Nginx
Access http://192.168.75.138/index.html: The default port is 80, so the request is sent to proxy server, because the longest match is location/, it is forwarded to proxied server to serve; the browser displays " Hello, nginx! "
Access http://192.168.75.138/basketball.jpg: The default port is 80, so the request is sent to proxy server, because the longest match is location ~\. (gif|jpg|png) $, so the file returned by proxy server is "/data/images" + "/basketball.jpg", that is,/data/images/basketball.jpg, the browser displays the picture.


Note:
    • In proxied server, the root of location is in its context, so that the reason for working is "internally covered externally", that is, if the location defines its own root and uses its own root, if not defined, Use the root of the nearest outer layer;
    • In proxy server, location ~\. The (gif|jpg|png) $ parameter is a regular expression that matches all URIs that end with. gif,. jpg,. png. Regular expressions with "~." Start (but need to change the meaning, so write "~\."). If a URI matches a regular expression, the matching string is equivalent to the entire URI, so it is the longest match and is served by this location.

Configuration Example 3:FASTCGI Agentsimilar to configuration Example 2, only the proxy service is fastcgi
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;    }}


Introduction to Nginx

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.