Nginx environment construction and front-end deployment tutorial (Windows version)

1. Introduction to Nginx

Nginx (engine x) is a lightweight, high-performance HTTP and reverse proxy web server, and also provides IMAP/POP3/SMTP services. It can almost achieve 7 * 24 hours of uninterrupted operation, even if it runs for several months, it does not need to be restarted, and it can also perform hot update of the software version under the condition of uninterrupted service. Performance is the most important consideration for Nginx. It occupies less memory, has strong concurrent capabilities, and can support up to 50,000 concurrent connections. Most importantly, Nginx is free and commercialized, and its configuration is relatively simple. Nginx improves the response speed of the website, optimizes the user experience, and increases the robustness of the website.

2. Features of Nginx

① reverse proxy

Add a transition server, that is, a reverse proxy, on the client and server sides. The client is unaware of the proxy, and the client can access without configuration. The client sends the request to the reverse proxy server, and the reverse proxy server selects the target server to obtain data according to the policy, and then returns to the client. What is exposed to the client is only the address of the reverse proxy server, thereby hiding the address of the real server.

②Load balancing

In layman's terms, it is to share the network pressure among multiple units. Load balancing is based on the existing network structure, providing a cheap, effective and transparent method to expand the bandwidth of network devices and servers, increase throughput, strengthen network data processing capabilities, and improve network flexibility and availability.

Nginx gives three ways of load balancing:

  1. Polling method (default method):

    Each request is allocated to different back-end servers one by one in chronological order, and if the back-end server hangs up, it can be automatically eliminated. It is suitable for server configuration, stateless and short, flat and fast services. It is also applicable to image server clusters and pure static page server clusters.

  2. weight Weight mode (weighted round robin):

    Specify the polling probability, the weight is proportional to the access ratio, and is used for uneven performance of the backend server

    Condition. This method is more flexible. When there are differences in the performance of the back-end servers, by configuring the weights, the performance of the servers can be fully utilized and resources can be used effectively. The weight is proportional to the access ratio, and it is used when the performance of the backend server is uneven. The higher the weight, the greater the probability of being visited

  3. ip_hash:

    The disadvantage of the first two methods is: in the load balancing system, if the user logs in on a server, the user may be relocated to other servers in the cluster when the user requests the second time, and the previous login information will be lost. lost. Therefore, the ip_hash command can be used to solve this problem. If the customer has already visited a certain server, when the user visits again, the request will be automatically located to the server through the hash algorithm. Each request is allocated according to the hash result of the access ip, so that each visitor accesses a backend server regularly, which can solve the session problem.

③Separation of static and dynamic

Nginx has strong static processing capability, but insufficient dynamic processing capability. Therefore, dynamic and static separation technology is commonly used in enterprises. Dynamic and static separation technology actually adopts a proxy method, adding a location with a regular match in the server{} section to specify the matching item. Dynamic and static separation for PHP: static pages are handed over to Nginx for processing, and dynamic pages are handed over to PHP-FPM module or Apache for processing . In the configuration of Nginx, the different processing methods of static and dynamic pages are realized through the location configuration section and regular matching.

3. Nginx installation

①Download the stable version from the official website

Download address: http://nginx.org/en/download.html

② Unzip to the installation directory

4. Nginx start

Startup method 1: double-click nginx.exe, after double-clicking, you can see a small black window flashing past.
Start method 2: Open the cmd command window, switch to the nginx decompression directory, enter the command nginx.exe, and press Enter

Note: If the installation directory is in Chinese, an error will be reported when opening the exe file.

Open the browser: enter the URL http://localhost:80 in the browser address bar   and press Enter, and the following page will appear to indicate that the startup is successful!

 

5. Nginx configuration monitoring

The configuration file of nginx is nginx.conf in the conf directory. The default configured nginx monitor port is 80. If port 80 is occupied, it can be changed to an unoccupied port.

When modifying the nginx configuration file nginx.conf, it is not necessary to restart nginx after closing nginx, just execute the command nginx -s reload to make the changes take effect

6. Nginx shutdown

Method 1: (1) Enter the nginx command nginx -s stop (quickly stop nginx) or nginx -s quit (complete and orderly stop nginx), the difference between these two commands is that nginx -s stop is to quickly stop Nginx, and nginx -s quit is to stop Nginx in an orderly manner, the former may cause the data to not be fully saved;

Method 2: (2) Use taskkill taskkill /f /t /im nginx.exe

Method 3: Task Manager Kills All Nginx Processes

7. Nginx common commands

nginx -s reopen #重启Nginx
nginx -s reload #重新加载Nginx配置文件,然后以优雅的方式重启Nginx
nginx -s stop #强制停止Nginx服务
nginx -s quit #优雅地停止Nginx服务(即处理完所有请求后再停止服务)
nginx -t #检测配置文件是否有语法错误,然后退出
nginx -?,-h #打开帮助信息
nginx -v #显示版本信息并退出
nginx -V #显示版本和配置选项信息,然后退出
nginx -t #检测配置文件是否有语法错误,然后退出
nginx -T #检测配置文件是否有语法错误,转储并退出
nginx -q #在检测配置文件期间屏蔽非错误信息
nginx -p prefix #设置前缀路径(默认是:/usr/share/nginx/)
nginx -c filename #设置配置文件(默认是:/etc/nginx/nginx.conf)
nginx -g directives #设置配置文件外的全局指令
killall nginx #杀死所有nginx进程

8. Nginx front-end deployment

① Take the above project as an example, copy the front-end project path

F:\learnany\springboot-ehr\ehr-admin

② Configure nginx.conf

Open the nginx.conf file under the conf folder under the nginx folder .

Find the root line under location, replace html with the path of the static web page and save it.

③Start Nginx

Browser access: http://localhost:80

 

Guess you like

Origin blog.csdn.net/zzj_csdn/article/details/128054264