Nginx has many benefits for servers, such as:

  1. Reverse proxy protects normal servers
  2. Load balancing, which can efficiently distribute server clusters
  3. You can do gzip compression
  4. You can add a cache

As a front-end, I’m trying to learn to move to the big front end, so I have to master some general nginx operations.

1. Install

1.1 Installation on MAC

Brew install nginx on MAC. After installation, you can go to the /usr/local/etc/nginx directory to find nginx.conf and modify the configuration.

Brew installed nginx with the following configuration:

Nginx configuration file path: / usr/local/etc/nginx/nginx. Conf nginx server page default path: / usr/local/var/WWW nginx installation path: / usr/local/Cellar/nginx / 1.15.5

1.2 Installation on Linux

Linux on the installation, it is recommended to look at the rookie tutorial, follow the tutorial down, OK.

www.runoob.com/linux/nginx…

Note: according to the novice tutorial to install nginx, his installation directory is in/usr/local/webserver/nginx this directory. We’re going to use this directory later.

2. Common nginx commands

Nignx functions mainly through configuration, but the common start, restart, and stop commands need to be known.

nginx   # start nginx
nginx -s reload     Reload the configuration file
nginx -s reopen     # restart nginx
nginx -s stop       # stop nginx
Copy the code

Once you have brew installed on your MAC, you can start Nginx directly using nginx commands.

But we cannot directly on Linux installed after operated by nginx, we need to/usr/local/webserver/nginx/sbin/the directory and execute the command.

3. Start the configuration

If we want to change the configuration of nginx, we first need to find the location of nginx.conf. And have some knowledge of nginx.conf. First look at where nginx.conf is located:

Modify the nginx. Conf:

The location of MAC/usr/local/etc/nginx/nginx. Conf, Linux if according to the installation of the novice tutorial, location in the/usr/local/webserver/nginx/conf/nginx. Conf.

Modify the startup page:

If you want to modify the nginx start page, MAC can go to/usr/local/var/WWW modify, Linux to/usr/local/webserver/nginx/HTML modify index. The HTML.

The content of nginx is roughly divided into the following blocks:

# global block
#worker_processes 2; # Number of processes allowed to generate. Default is 1
#pid /nginx/pid/nginx.pid; # specify the location where nginx run files are stored
error_log log/error.log debug;  # Specify the log output path
events {   # events block
    worker_connections 1024;  # Maximum number of connections. } http {# HTTP block.# HTTP global block
    server {   # server block.Server global block
        listen  9090;  # request port
        server_name  localhost;  # request domain name
        location [PATTERN] {  # the location of block. root html;# file root directory
            index  index.html index.htm; Set up the access pageDeny 127.0.0.1;Rejected IPAllow 172.18.5.54;# Allowed IP
        }
        location [PATTERN] {
            ...
        }
    }
    server {
        ...
    }
    upstream [NAME] {  Proxy server load balancing group. }...# HTTP global block
}
Copy the code
  1. Global block: Configures directives that affect nginx globally. Generally there are your user group running nginx server, nginx process PID storage path, log storage path, configuration file import, allowed to generate worker process number, etc.
  2. Events block: The configuration affects the Nginx server or network connection to the user. There are the maximum number of connections per process, which event-driven model to process connection requests, whether to accept multiple network connections at the same time, and enable serialization of multiple network connections.
  3. HTTP block: you can configure multiple servers, configure proxy, cache, log definition and most functions and third-party module configuration. Such as file import, mini-type definition, log customization, whether to use SendFile to transfer files, connection timeout, number of single connection requests, etc.
  4. Server block: configure the parameters of the virtual host, one HTTP can have multiple servers.
  5. Location block: Configures the routing of the request and the processing of the various pages.
  6. Upstream: configures proxy server groups, including load balancing.

If you don’t understand the configuration, you can then go down to configure nginx for your own server, try it suddenly enlightened.

3.1 Reverse Proxy and Load Balancing

Go to nginx.conf and configure routing in the server.

server {
    listen       9090;
    server_name  localhost;

    #charset koi8-r;

    #access_log logs/host.access.log main;location / { proxy_pass http://tomcats; }}Copy the code

Proxy_pass refers to the specific server group to which the route should be forwarded. Here we set a tomcats group and add an upstream module to the same level as server. The module is named Tomcats and the IP server to which the route should be forwarded is configured. Multiple IP addresses can be configured. If no allocation policy is configured for multiple IP addresses, the IP addresses are allocated in polling mode by default.

In this way, reverse proxy and load balancing are implemented. Nginx proxies its own servers to several IP servers, and the servers evenly distribute incoming traffic.

Upstream tomcats {server 192.168.100.100:8080; Server 192.168.100.101; server example.com:8080; }Copy the code

We can also set different allocation policies for each server.

Upstream tomcats {server 192.168.100.100:8080 weight=2;# 2/6 timeServer 192.168.100.101 weight = 3;# 3/6 time
    server example.com:8080 weight=1;  # 1/6 time
}
Copy the code

As shown in the figure below, the polling without weighting is on the left, and the polling after weighting is on the right, which will be allocated according to the weighting.

3.2 Configuring IP Cache

We can use ip_hash to assign results to each IP address. That is, after an IP address is accessed, its subsequent accesses will be fixed to the same machine, which helps to solve the problem of session non-sharing.

The configuration is simple:

upstream tomcats {
    ip_hash;
    server 192.168.100.100:8080;
    server 192.168.100.101;
    server example.com:8080;
}
Copy the code

The configuration is as follows:

Nginx can also enable gzip compression

When editing, search gzip to find the corresponding configuration information, which is commented by default, that is, turned off. Let’s just turn off the comments.

Read the last two songs

  1. Please give a thumbs-up to your favorite friends. If you feel helpful to people around you, please share it with your fingers. Thank you so much for taking the time to read this, and thank you for your likes and shares.
  2. I hope you pay attention to my public account, the first time the new article to the public account, the public account mainly sent some personal essays, reading notes, and some technical hot spots and real-time hot spots, and there is a very attractive my own money lottery oh ~