Nginx learning notes (entry version)

Posted by sgarcia on Mon, 07 Mar 2022 14:38:09 +0100

1, Installing Nginx under Centos

Reference documents

If the machine installs Nginx for the first time, you need to set the software package warehouse of Nginx before installing Nginx, and then install and update Nginx through the warehouse

  • Install some pre required configurations
yum install yum-utils
  • Set the yum warehouse in / etc / yum repos. D create an nginx Repo file
    • The content of the document is
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

The stable version is used by default

To use the development version, you can set it through Yum config Manager -- enable nginx mainline.

  • Install Nginx
yum install nginx

2, Getting started with Nginx

Reference documents

Nginx configuration file nginx Storage location (or relationship) of conf:

  • /usr/local/nginx/conf
  • /etc/nginx
  • /usr/local/etc/nginx

Start, stop and reload profiles for Nginx

Start Nginx

nginx

Stop Nginx

# Stop immediately
nginx -s stop
# It will not stop until nginx has processed the request being serviced
nginx -s quit

Reload profile

nginx -s reload

After the configuration file is modified, if the Nginx server is not restarted or the configuration file is reloaded, the modified configuration will not take effect.

When using nginx -s reload to reload the configuration file, the main process of Nginx will check the syntax of the new configuration file and use the new configuration information;

If the above operation is successful:

  • The main process will start a new working process
  • The main process will send a message to the old working process asking them to stop working. When the working process receives the command to stop working, it will not receive new requests, but will only serve the requests currently being served. When all requests are processed, the old working process will be closed.

If the above operation fails:

  • The main process will roll back the changes and still use the configuration information in the old configuration file

During reload, the Nginx service will not stop.

Reopen the log file

nginx -s reopen

View the version of Nginx

nginx -v

3, Nginx as a static resource server

Reference documents

  • preparation

    • Create the / data/www folder and create an index in it HTML file
    <!-- index.html Document content -->
    <h1>Hello Nginx</h1>
    
    • Create a / data/images folder and put some picture resources in it
  • configuration information

# In nginx Add the following configuration to the http block of the conf configuration file
server {
	# /(prefix) indicates that when the requested URI and / match, the requested URI will be spliced with the path specified by root to obtain the final resource path
    location / {
            root /data/www;
    }

    location /images {
            root /data;
    }
}
# When more than one location matches the requested URI, select the location with a longer prefix
  • Reload the configuration file for Nginx
nginx -s reload
  • test

4, Nginx as reverse proxy server

Reference documents

  • preparation
    • Create the / data/up1 folder and create the index HTML file
  • configuration information
# Proxy server information
server {
		# handle http://localhost:80 request
        location / {
        		# Configure the address of the proxy server
                proxy_pass http://localhost:8080;
        }

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

# Information of proxy server
server{
    listen 8080;
    root /data/up1;

    location / {

    }
}
  • Reload profile
  • test

Reverse proxy case 1

Target: enter www.test.com in the Windows browser COM, the page jumps to the home page of Tomcat server in Centos

  • Configure the mapping relationship between IP address and domain name

Edit the C:/Windows/System32/drivers/etc/hosts file and add the following information

# Mapping relationship between IP address and domain name
192.168.48.128 www.test.com
  • configuration information
server {
        listen 80;
        server_name 192.168.48.128;
        
        location / {
        		# Address of proxy server
                proxy_pass http://localhost:8080;
                root /data;

                index index.html index.htm;
        }
    }
  • Reload profile
  • test

Reverse agency case 2

Goal: use nginx reverse proxy to jump to services on different ports according to the access path

visit http://192.168.48.128:9001/edu/ Jump directly to 192.168.48.128:8080
visit http://192.168.48.128:9001/vod/ Jump directly to 192.168.48.128:8081

  • to configure
server {
		# Listening to port 9001
        listen 9001;

        server_name 192.168.48.128;
        # Using regular matching,
        location ~ /edu {
        		# Specify the address of the load balancer
                proxy_pass http://localhost:8080;
        }

        location ~ /vod {
                proxy_pass http://localhost:8081;
        }
    }

5, Nginx for load balancing

http {
	# Configure the server cluster. myserver is the name of the server cluster
	# The proxy server information is configured here
    upstream myserver{
        server 192.168.48.128:8080 weight=5;
        server 192.168.48.128:8081;
    }

    server {
        listen 80;

        server_name 192.168.48.128;
        location / {
                proxy_pass http://myserver;
        }
    }
}

erver{
server 192.168.48.128:8080 weight=5;
server 192.168.48.128:8081;
}

server {
    listen 80;

    server_name 192.168.48.128;
    location / {
            proxy_pass http://myserver;
    }
}

}

Topics: Java Linux Nginx