Articles and codes have been archived in [Github warehouse: https://github.com/timerring/front-end-tutorial ] or the official account [AIShareLab] can also be obtained by replying to nginx .
Article directory
Directory Structure
Enter the main directory of Nginx with the following folders
client_body_temp conf fastcgi_temp html logs proxy_temp sbin scgi_temp uwsgi_temp
The folder ending with _temp
is used to store temporary files during operation.
The other main folders are:
- conf: used to store configuration files
- html: the default directory html, css, etc. used to store static files
- sbin: the main program of nginx
- logs: store various logs, such as access record access related records, error record error records, nginx.pid records the pid number of the service, that is, the process id number.
Basic operating principle
There are multiple processes in total, and one of the main processes, Master, is responsible for reading and verifying configuration files.
The sub-process Worker is the corresponding access request.
Nginx configuration and application scenarios
First of all, the focus is on the configuration file of Nginx nginx.conf
, which has a large part of comment configuration. Here, we first focus on the minimum configuration required by nginx.
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
minimum configuration
worker_processes
worker_processes 1;
The default is 1, which means to start a business process, corresponding to the number of cores of the physical CPU of the server, and one CPU core corresponds to one worker_processes. Of course, the number of worker_processes can also be increased, but for the same CPU, scheduling is required, and the efficiency decreases instead.
Under the events module
worker_connections
worker_connections 1024;
A single business process can accept the number of connections.
Under the http module
include mime.types;
include mime.types;
Introduce the http mime type and add it to the http header to indicate the format that the browser should parse.
For example:
application/octet-stream bin exe dll;
It is to specify the browser, analyze the exe and other types in the form of data stream, and download it.
image/jpeg jpeg jpg;
It is to directly let the browser display it as an image.
default_type application/octet-stream;
If mime
the type does not match, the binary stream is used by default.
The request information is received by the operating system, and the network interface of the operating system forwards the request to Nginx (the registration port is bound before the request). If it sendfile on;
is closed, Nginx first reads the files on the SSD to the application according to the configuration file, and then sends them to the network interface of the operating system (that is, the driver of the network card). This process will be scheduled, the cache of the network card and the cache of the kernel, Layer-by-layer cache replication.
But if it is turned on sendfile on;
, it will send a signal directly to let the network interface read the file.
keepalive_timeout 65;
Keep the connection timeout, the reverse proxy stage will be explained in detail.
Under the server module
nginx can configure multiple servers, and a server is a host.
Virtual host configuration
server {
listen 80; 监听端口号
server_name localhost; 主机名
location / { 匹配路径
root html; 文件根目录
index index.html index.htm; 默认页名称
}
error_page 500 502 503 504 /50x.html; 报错编码对应页面
location = /50x.html {
root html;
}
}
listen 80;
The listening port number of each host is different and does not interfere with each other. Each of these hosts also becomes a virtual host (vhost).
server_name localhost;
Host name (you must write a resolvable host name, for example, localhost is defined as 127.0.0.1 in the host file of this machine. Or it can be changed to a domain name)
location
Match path, used to match uri. Usually the complete link is called url: http://123.com/456/index.html
and uri refers to /456/index.html
this part.
root html;
The root directory of the file, here is a relative path.
index index.html index.htm;
The default page name, where index is index.html or index.htm.
error_page 500 502 503 504 /50x.html;
The page corresponding to the error code, usually returns an error such as 500, and will automatically jump to http://123.com/50x.html
the page.
And if there is no such page, according to the following logic, it will automatically jump to the root (that is, the html directory) to find the page.
location = /50x.html {
root html;
}
Nginx gets the IP address and initiates TCP/IP from the DNS server. The TCP/IP protocol can only transmit some binary data, which is sent to the target server in the form of data stream. The HTTP protocol is above the TCP/IP protocol, and the underlying protocol TCP/IP has no constraints. However, the HTTP protocol implements the terminator, how long the requested data message is, and so on. Another protocol, the https protocol, is based on the http protocol, adding an additional layer of data security. Because when surfing the Internet, we will go through many gateways, such as the router in our home, as well as the gateway of the community, the gateway of the service provider, and finally the gateway of China Unicom. From the gateway at the district level, to the gateway at the city level, and then to the national gateway, the security is better after encryption.