Introduction to the installation of nginx and nginx configuration of Centos7 yum source

Posted by linus on Mon, 20 Jan 2020 11:51:17 +0100

Introduction to the installation of nginx and nginx configuration of Centos7 yum source

(1) Installation

1.1 add Nginx repository
yum install epel-release
1.2 start to install nginx
yum install nginx

In the middle of the way, these two commands will prompt you whether to continue or not. You only need to go all the way to yyyyyyyyyy

Here nginx is installed

1.3nginx startup points for attention

1. Whether the nginx port is occupied? The default port of nginx is 80

2. Whether the server opens the 80 port protection wall and the corresponding security group

The following command is to open the 80 port firewall. To set the security group for ECS, you need to log in to Alibaba cloud or Tencent cloud open security group

firewall-cmd --zone=public --add-port=80/tcp --permanent

Start and try!!

With nginx common commands

#Start nginx both commands can be started
systemctl start nginx   
nginx

#Setting nginx startup
systemctl enable nginx

#Uninstall nginx command
yum remove nginx

#Stop nginx
nginx -s stop

#Restart nginx
nginx -s reload

If you do not modify the default port of nginx, you can directly access the server Ip (80 can not be written by default) after the startup of nginx. If you modify the default port, the access method is server ip:nginx port

After visiting the webpage, a prompt will show that nginx has been successfully installed and started

Welcome to CentOS
The Community ENTerprise Operating System

(2) nginx configuration

The default location of nginx configuration installed using yum source is

/etc/nginx/nginx.conf
# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/
#Running user
user nginx;
#Start the process, and each Nginx process consumes an average of 10M~12M memory. It is recommended to specify the same number of CPU s
worker_processes auto;
#Global error log and PID file
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
#Working mode
events {
	#Maximum number of concurrent links for a single background worker process
    worker_connections 1024;
}
#http server, load balancing and reverse proxy use it
http {
    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  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;
    
	#Set the MIME type, which is defined by the mime.type file
    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;
    
    #The server block is the configuration of the virtual host. The server flag defines the start of the virtual host
    server {
    	#listen is used to specify the service port of the virtual host. After startup, the virtual machine ip + the specified port is used for access
        listen       80 default_server;
        listen       [::]:80 default_server;
        #Server name is used to specify the IP address or domain name. Multiple domain names are separated by spaces
        server_name  _;
        #The root command is used to specify the web page root directory of the virtual host, which can be a relative path or an absolute path
        root         /usr/share/nginx/html;

        #Load the configuration file for the default server group
        include /etc/nginx/default.d/*.conf;
		#Block location
		#URL address matching / a access ip: port / A 
		#The location below is my current configuration
		#Multiple location s can be configured in a server, but "/" must have one
        location / {
        #ftp file home directory, only one is allowed
        root /var/ftp/pub/;
        #The first page of this visit
        index index.html index.htm index.php index.jpg index.mp4 index.txt;
        }
        #404 pages
        error_page 404 /404.html;
            location = /40x.html {
        }
		#500 pages
        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
        #Second location
        location /chexuan/ {
        alias /ftp/chexuan/;
        index index.html index.htm index.php index.jpg index.mp4 index.txt;
        }

        #Give an example
        #Accessing the root directory /, such as http://localhost /, will match the index file under / var/ftp/pub /
        #Access the directory / Cheyuan / for example, http: / / localhost / Cheyuan / will match the index file under / ftp / Cheyuan /
    }

# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2 default_server;
#        listen       [::]:443 ssl http2 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers HIGH:!aNULL:!MD5;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / {
#        }
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

}


The difference between root and alias in the location code block

root instance:

location /t/ {
     root /www/root/html/;
}
If the URI of a request is / t/a.html, the web server will return the file of / www/root/html/t/a.html on the server.

alias instance:

location /t/ {
 alias /www/root/html/leilei/;
}
If the URI of a request is / t/a.html, the web server will return the file of / www/root/html/leilei/a.html on the server. Note that this is leilei, because alias will discard the path configured after location and point the currently matched directory to the specified directory.


Be careful:

  1. When using alias, the directory name must be followed by "/".
  2. When using regular matching, alias must capture the content to be matched and use it at the specified content.
  3. alias can only be in a location block. (root can not be placed in location)

At present, the learning of Nginx is over. We will continue to update it later

Published 29 original articles, won praise, 22 visits, 3441
Private letter follow

Topics: Nginx yum ftp SSL