1. The initial Nginx

1.1 Introduction to Nginx

Nginx is a lightweight HTTP server with an event-driven asynchronous non-blocking processing framework, which gives it excellent IO performance and is often used for server-side reverse proxies and load balancing.

1.2 Advantages of Nginx
  • Supports massive and high concurrency: uses I/O multiplexing epoll. Nginx was officially tested to support up to 50,000 concurrent connections, and production environments can support up to 20,000 to 40,000 concurrent connections.
  • Low memory consumption: Nginx currently has the lowest memory consumption among mainstream servers. For example, when we use Nginx+PHP, starting 10 Nginx processes with 30,000 concurrent links consumes 150M of memory.
  • Free to use and commercialized: Nginx is open source software that uses the 2-clause BsD-like protocol and can be used free of charge and commercially.
  • Simple configuration files: Network and program configurations are easy to understand, even for non-professional operation and maintenance.

2. Quick build of Nginx

  • Install using Homebrew
brew install nginx
Copy the code
  • Check whether the installation is successful
nginx -v
Copy the code

3. The Nginx service starts, stops, and restarts

3.1 start
nginx
Copy the code

Check the service startup status

ps -A | grep nginx
Copy the code

3.2 stop
  • Kill command

In the end the ps – A | grep nginx inspection process

kill-QUIT Indicates the process ID. // The process will not be stopped immediatelyCopy the code
kill-TERM Process ID // Stop immediatelyCopy the code
kill-INT Indicates the process ID. // Stops immediatelyCopy the code
  • nginx -s stop

Stop now: This approach is tough and terminates the process whether it is working or not.

  • nginx -s quit

Leisurely stop: This method is less aggressive than stop and requires the process to finish its current work before stopping

3.3 restart
nginx -s reload
Copy the code
nginx -t
Copy the code

4. Nginx configuration file

4.1 Configuration File
vim /usr/local/etc/nginx/nginx.conf // View the configuration fileCopy the code
# 1. Global modules
#user nobody;
worker_processes  1; # Number of processes allowed to generate. Default is 1

#error_log logs/error.log; Specify log path, level. , this setting can fit into a global, HTTP server block, level as: debug | info | notice | warn | error | crit | alert | emerg
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid; # specify the location where nginx run files are stored

# 2. Events module
events {
    worker_connections  1024; # Maximum number of connections, default is 512
}

# 3. The HTTP module
http {
    include       mime.types; File extension and file type mapping table
    default_type  application/octet-stream; The default file type is text/plain

    #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 logs/access.log main;

    sendfile        on;
    #tcp_nopush on;
    keepalive_timeout  65; Connection timeout

    #gzip on;
    
    # 4. Server global block
    server {
        listen       8080; # monitor port number
        server_name  localhost;  # monitor address
        
        # 5. The location
        location / {
            root   html;
            index  index.html index.htm; The default startup directory for the service
        }

        #error_page 404 /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html; Error status code display page, need to restart after configuration
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        # proxy_pass http://127.0.0.1; # Request to http://127.0.0.1 server list
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        # root html;
        # fastcgi_pass 127.0.0.1:9000;
        # fastcgi_index index.php;
        # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
        # include fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        # deny all; IP address denied
        # allow all; The IP address allowed to access
        #}}}Copy the code

In the above configuration, the Nginx configuration file consists of the following parts:

  • Global block: Configures directives that affect nginx globally. Generally, there are user groups running nginx server, nginx process PID storage path, log storage path, configuration file import, allowed number of worker processes, etc.
  • Events block: The configuration affects the Nginx server or network connection to the user. The maximum number of connections per process, which event-driven model to choose to handle connection requests, whether to allow simultaneous acceptance of multiple network connections, enable serialization of multiple network connections, etc.
  • HTTP block: can nest multiple servers, configure proxy, cache, log definition and most functions and third-party module configuration. Such as file import, miME-type definition, log customization, whether to transfer files using SendFile, connection timeout, number of single connection requests, etc.
  • Server block: configure the parameters of the virtual host, one HTTP can have multiple servers.
  • Location block: Configures the routing of the request and the processing of the various pages.
4.2 Customizing error pages
  • Multiple errors specify a page

In the configuration file, we can see that

Error_page 500 502 503 504/50x.html;Copy the code

The error_page directive is used to customize the error page, 500,502,503,504. These are common error codes in HTTP, and /50.html is used to indicate that when any specified error occurs, it will be processed in the /50.html file at the root of the website.

  • Specifies separately for error pages
error_page 404 /404.html
Copy the code

A 404.html is automatically created and some information is written

vim /usr/local/var/www/404.html
Copy the code

Restart the service and visit again, and the 404 page will change.

4.3 Access Permissions for Nginx
location {
    deny: all; IP address denied
    allow: all; The IP address allowed to access
}
Copy the code

5. Nginx configures virtual hosts

5.1 Configuring Virtual Hosts Based on port Numbers

Modify the Server option in the configuration file so that there are two servers

server { listen 8088; Server_name localhost; location / { root html; index 8088.html 8088.htm; }}Copy the code

5.2 IP-based Virtual Hosts
server { listen 8088; Server_name 192.168.60.52; // Set IP location / {root HTML; index 8088.html 8088.htm; }}Copy the code

5.3 Using domain Names to Set Virtual Hosts