1 Nginx server overview
Nginx is a kind of server software. Its main and basic function is that it can be combined with server hardware (computer), so that programmers can publish programs on nginx server, so that thousands of users can browse.
In addition, Nginx is also a high-performance HTTP and reverse proxy server, as well as a proxy mail server. In other words, we can:
- Can publish websites (static, html,css,js)
- Load balancing can be realized,
- proxy server
- It can be used as a mail server to realize the functions of sending and receiving mail
2 using Nginx on Linux
1) Download Nginx
get into http://nginx.org/ Website, Download nginx-1.17.5 tar. GZ file
2) Upload to virtual machine
Use the client to download nginx-1.17.5 tar. Upload the GZ file to the home directory.
View with command
# cd home # ll
3) Prepare dependent environment
#The installation of Nginx depends on the environment, and - y means that y is selected by default for all prompts
yum -y install pcre pcre-devel yum ‐y install zlib zlib‐devel yum ‐y install openssl openssl‐devel
4) Extract and compile the installation
# Enter the home directory and unzip it tar -zxvf nginx-1.17.5.tar.gz -C /home # Enter nginx directory cd nginx-1.17.5 # Compile and install [gcc compilation environment already exists] ./configure make make install # After successful installation, an nginx directory will be added under / usr/local
5) Start server
#Enter the sbin directory of nginx cd /usr/local/nginx/sbin #Start in sbin directory ./nginx #Stop in sbin directory ./nginx ‐s stop #Rewrite loading in sbin directory ./nginx ‐s reload #Open port 80 for external access of Linux. By default, Linux will not open port 80 #To edit iptables files, refer to 4.5 #Check whether there are nginx threads and whether they exist ps ‐ef | grep nginx
6) Browser access
The browser inputs the ip address of the virtual machine. The default is port 80
3 Nginx configuration file structure
If you have downloaded your installation files, you might as well open Nginx.exe in the conf folder Conf file, the basic configuration of Nginx server, and the default configuration are also stored here.
In nginx Annotation symbol bit of conf#
File structure nginx
... #Global block events { #events block ... } http #http block { ... #http global block server #server block { ... #server global block location [PATTERN] #location block { ... } location [PATTERN] { ... } } server { ... } ... #http global block }
1. Global block: configure instructions that affect nginx global. Generally, there are user groups running nginx server, pid storage path of nginx process, log storage path, introduction of configuration file, number of worker process es allowed to be generated, etc.
2. events block: the configuration affects the nginx server or the network connection with the user. There is the maximum number of connections per process, which event driven model is selected to process connection requests, whether it is allowed to accept multiple network connections at the same time, and start the serialization of multiple network connections.
3. http block: it can nest multiple server s, configure most functions such as proxy, cache, log definition and the configuration of third-party modules. Such as file import, MIME type definition, log customization, whether to use sendfile to transfer files, connection timeout, number of single connection requests, etc.
4. server block: configure the relevant parameters of the virtual host. There can be multiple servers in one http.
5. location block: configure the routing of requests and the processing of various pages.
Here is a configuration file for your understanding.
########### Each instruction must end with a semicolon.################# #user administrator administrators; #Configure users or groups. The default is nobody. #worker_processes 2; #The number of processes allowed to be generated. The default is 1 #pid /nginx/pid/nginx.pid; #Specify the storage address of nginx process running files error_log log/error.log debug; #Make log path and level. This setting can be put into the global block, http block and server block. The level is: debug|info|notice|warn|error|crit|alert|emerg events { accept_mutex on; #Set the serialization of network connection to prevent group panic. The default is on multi_accept on; #Set whether a process accepts multiple network connections at the same time. The default is off #use epoll; #Event driven model, select|poll|kqueue|epoll|resig|/dev/poll|eventport worker_connections 1024; #The maximum number of connections is 512 by default } http { include mime.types; #File extension and file type mapping table default_type application/octet-stream; #The default file type is text/plain #access_log off; #Cancel service log log_format myFormat '$remote_addr–$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for'; #Custom format access_log log/access.log myFormat; #combined is the default value for log format sendfile on; #It is allowed to transfer files in sendfile mode, which is off by default. It can be in http block, server block and location block. sendfile_max_chunk 100k; #The number of transfers per call of each process cannot be greater than the set value. The default value is 0, that is, there is no upper limit. keepalive_timeout 65; #The connection timeout, which is 75s by default, can be set in http, server and location blocks. upstream mysvr { server 127.0.0.1:7878; server 192.168.10.121:3333 backup; #Hot standby } error_page 404 https://www.baidu.com; # Error page server { keepalive_requests 120; #Maximum number of single connection requests. listen 4545; #Listening port server_name 127.0.0.1; #Listening address location ~*^.+$ { #Request url filtering, regular matching, ~ is case sensitive, ~ * is case insensitive. #root path; #root directory #index vv.txt; #Set default page proxy_pass http://mysvr; # The request goes to the list of servers defined by mysvr deny 127.0.0.1; #Rejected ip allow 172.18.5.54; #Allowed ip } } }
The above is the basic configuration of nginx. The following points need to be noted:
-
Several common configuration items:
- $remote_addr and $http_x_forwarded_for is used to record the ip address of the client;
- $remote_user: used to record the client user name;
- $time_local: used to record access time and time zone;
- $request: used to record the url and http protocol of the request;
- $status: used to record request status; Success is 200;
- $body_bytes_s ent: record the size of the main content of the file sent to the client;
- $http_referer: used to record the links accessed from that page;
- $http_user_agent: record the relevant information of the client browser;
-
Group startling phenomenon: when a network connection arrives, multiple sleeping processes are awakened at the same time, but only one process can get the link, which will affect the system performance.
-
Each instruction must end with a semicolon.