Nginx deployment website


Due to the tight front-end task, I helped deploy the front-end page. I have heard the name of nginx for a long time, so I preliminarily selected the scheme of nginx

Installing nginx

Environment: CentOS 7.4
Note: all commands in this article are executed under the root user. If you are an ordinary user, please add sudo
The installation is very simple. You can do it directly with one command (however, the latest version installed by Yum is generally not the latest version. If you want to install the latest version, please download and install it yourself):

yum install -y nginx

The premise is that you can connect to the Internet. If you can’t connect to the Internet, you can download and install the installation package in other ways. It’s not described here. There are many posts on the Internet for reference.

Configure nginx

If you do not know the configuration file path of nginx, you can view it with the following command:

nginx -t
[[email protected]]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Open the configuration file / etc / nginx / nginx conf
Modify the content of the corresponding server module

    server {
        listen       8080 default_server;
        #listen       [::]:80 default_server;
        server_name  _;
        #root         /usr/share/nginx/html;
        root          /home/front/dist;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        root          /home/front/dist;

        error_page 404 /404.html;
            location = /40x.html {

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {

Main modification points:
1. Port. This is optional. The default is 80. It is changed to 8080 here
2. The IPv6 address listening address is commented out. This is optional and can be adjusted according to the needs
3、server_ Name should be the corresponding domain name. It’s not used here. It doesn’t move
4、root /home/front/dist; This indicates the web access directory, which needs to be changed to the directory where your web page is located
5. Include has not been changed, nor has it studied its role
6. Root / home / front / dist is added to location;
7、error_ Page uses nginx by default and has not been modified

Save and exit after modification

start nginx

Execute command

[[email protected]]# nginx

Then visit the page and find the error:
Nginx deployment website
At first, I thought there was a problem with the configuration, searched for various configuration schemes, found that they were inaccessible, and finally found a solution:

[[email protected] /home/front]# vi /etc/nginx/nginx.conf

# For more information on configuration, see:
#   * Official English Documentation:
#   * Official Russian Documentation:

user nginx;

Because of the permission problem, I started nginx with the root user, but the default user in the configuration is nginx, so change its user nginx to root.
Reload the configuration and start nginx

nginx -s reload

be accomplished.
However, these are used for local access without secure connection. If you want to use secure connection, you need to configure certificate. At the same time, HTTP 1.1 is used here. If you want to use HTTP 2.0 protocol, you also need to configure http2.0

Nginx common commands

Use nginx help to view

[[email protected] /home/front]# nginx -h
nginx version: nginx/1.16.1
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]

  -?,-h         : this help
  -v            : show version and exit
  -V            : show version and configure options then exit
  -t            : test configuration and exit
  -T            : test configuration, dump it and exit
  -q            : suppress non-error messages during configuration testing
  -s signal     : send signal to a master process: stop, quit, reopen, reload
  -p prefix     : set prefix path (default: /usr/share/nginx/)
  -c filename   : set configuration file (default: /etc/nginx/nginx.conf)
  -g directives : set global directives out of configuration file

Nginx – t: it has been used above to test whether the configuration file has syntax errors
Nginx – s: it mainly sends signals to the nginx main process for processing. The help information also lists the signal names: stop, quit, reopen, reload

Nginx – s stop: forcibly stop the nginx service
Nginx – s quit: gracefully stop the nginx service (that is, stop the service after processing all requests)
Nginx – s reopen: restart nginx
Nginx – s reload: reload the nginx configuration file and restart nginx gracefully

reference resources