preface
How to set up Nginx server on Ali Cloud server? .
Ningx introduction
Nginx is a lightweight Web server, with a small memory footprint, very fast startup, high concurrency advantages, using the event driven asynchronous non-blocking processing framework, IO performance is good, often used in the server side reverse proxy and load balancing.
Forward proxy and reverse proxy
What is a forward proxy? What is a reverse proxy? “Forward proxy” “For example” “Because of the firewall, direct access to Google is not accessible, then you can use a ladder, namely VPN, so this VPN (proxy) is to access the server on behalf of the client, and then return data to the client. The server does not know the target client, but simply establishes a connection with the VPN. For example, when you (client) go to rent a house, the real landlord (server) will rent to the second-hand room east (agent server), and you rent the house through the second-hand room east, that is, you and the second-hand room east establish contact, rent to the house, is not the real landlord. In a word: “forward proxy proxy client, reverse proxy proxy server.”
Basic configuration
After successful installation, normally you the conf directory of the configuration file is/etc/nginx/nginx. Conf, if is the machine, the path in the/usr/local/nginx/conf/nginx. Conf (path) under the condition of unchanged. If you don’t know where, can the following command help you
ps aux|grep nginx
Copy the code
vim /etc/nginx/nginx.conf
To view the CONF file configuration, run vim
The basic structure of an nginx configuration file is as follows:
❝
❝“Main” : indicates the global configuration of nginx. Events: The configuration affects the network connection between the Nginx server and users. “HTTP” : allows you to nest multiple servers, configure proxy, cache, log definition and most functions, and configure third-party modules. Server: Configure virtual host parameters. One HTTP server can contain multiple servers. Location: Configure the route of the request and the processing of various pages. Upstream: Specify the IP address of the back-end server, which is an integral part of load balancing configuration.
❞
❞
Virtual host
An HTTP server can have multiple servers, i.e. multiple virtual hosts, as follows:
server {
listen 80;
server_name localhost;
root html;
index index.html index.htm;
} Copy the code
“Analysis” :
- Nginx listens on port 80 by default and can listen on other port numbers as well
- Server_name is the name of your server and can be matched exactly, wildcard or regular. It is usually first come, first served.
- /var/local/marking-h5,n: /var/local/marking-h5
root /var/local/marking-h5
(Build the packaged dist file on the server path) - Index The entry to your project, usually index.html.
❝
❝Each configuration must end with a semicolon (;). 2. Save and exit the configuration, and run the nginx -s reload command to restart the configuration
2. After nginx -s reload is executed, it is not clear whether it is enabled or not
ps -ef |grep nginx
Check the process
❞
❞
Project file mapping
The configuration of access to project files is mainly root and alias instructions, “alias” alias followed by the specified directory is accurate, and the end must be /. Root Indicates the parent directory, and the parent directory must contain the directory with the same name as the directory specified after location.
Cross domain
Cross domain definition
Cross-domain exists because of the same origin policy (which restricts how an Origin document or the script it loads can interact with resources from another source). It can help block malicious documents, reduce the presence of possible attack media), it limits the protocol, domain name, port number to be the same to normal access, as long as there is a difference, that is cross-domain.
Nginx handles cross-domain problems
Location can be used to route requests, so cross-domain problems start with it.
server {
listen 80;
server_name localhost;
location /api/ {
// When a request is made to the server, a match is made to the API
proxy_pass https://credit.ccmallv2.create-chain.net/api/; } } Copy the code
Nginx addresses cross-domain principles
Cross-domain exists because of “different sources”. Front-end projects and back-end projects are placed on different servers or have different domain name configurations. For example, when front-end project A.baidu.com visits back-end project B.baidu.com, cross-domain will naturally occur. And “configure nginx location to intercept cross-domain requests in the front end, and finally broker requests back to B.baidu.com”. This is a perfect way to avoid the same origin policy. When A.baidu.com visits Nginx, a.baidu.com belongs to the same origin access, and nginx does not trigger the browser’s same origin policy when forwarding requests to the server.
Load balancing
The above cross-domain processing is a chestnut of reverse proxy. CDN service is also one of the application scenarios of reverse proxy. In addition, reverse proxy is also the basis of load balancing.
Function of load balancing
- “Distribute server Cluster stress” In a server cluster, Nginx can distribute client requests “evenly” (not strictly, but by setting weights) to all servers in the cluster.
- “Ensure client Access stability” Nginx also has a health check (server heartbeat check) function, which periodically polls all servers in the cluster to send health check requests to check whether any servers in the cluster are abnormal. Once a server is found abnormal, it is removed.
Configuring Load Balancing
proxy_pass http://backserver/;
upstream backserver{
ip_hash;
server 127.0. 01.:9090 down; //(Down indicates that the server does not participate in the load temporarily.)
server 127.0. 01.:8080 weight=2; //(weight defaults to 1. The greater the weight, the greater the weight of the load)
server 127.0. 01.:6060; server 127.0. 01.:7070 backup; // If all other non-backup machines are down or busy, request backup machines. } Copy the code
Allocation policy
- Polling (Default) Each request is allocated to a different backend server one by one in chronological order. If the backend server goes down, the request is automatically removed.
- Weight (Weighted Polling) specifies the polling probability. Weight is proportional to the access ratio and is used when the back-end server performance is uneven.
- “Ip_hash” Each request is allocated according to the hash result of the access IP address. In this way, each visitor accesses the same back-end server, which can solve the session problem
- “Fair” allocates requests based on the response time of the back-end server, with priority given to those with short response times.
- Url_hash allocates requests based on the hash result of the url, so that each URL is directed to the same (corresponding) backend server. It is more effective when the backend server is used for caching.
❝
❝tips: Back – end server status down: the current server does not participate in load balancing Backup: the backup server when other nodes are unavailable max_fails: Specifies the number of requests that are allowed to fail, and fail_timeout: After max_fails fails, the server’s pause time, by default, is 10smax_conns: limits the maximum number of connections that each server can receive
❞
❞
The specified page
If the default hash mode is used, the front-end project can be refreshed normally. However, in the history mode, a 404 error is displayed. The official document has given an explanation and it is unnecessary to repeat it.
Nginx configuration
server {
listen 8090;
server_name localhost;
location / {
try_files $uri $uri/ /index.html;
index index.html index.html; } root /var/local/wechat-marking-app; } Copy the code
**try_files $uri $uri/ /index.html; **, meaning that if no static resource is matched, the index.html page is returned, which is the entry file of the page.
Alternatively, nginx can also “specify error pages”
# Return the error page based on the status code
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /source/error_page;
}
Copy the code
The related documents
Nginx: Nginx: Nginx: Nginx: Nginx: Nginx: Nginx: Nginx
Friendly tips:
Iterm2 is recommended for MAC users and cmder is recommended for Windows users to better use shell, ZSH, and bash scripts.
The 04.07 update
Filling holes guide
[WARN] Conflicting server name “localhost” on 0.0.0.0:8088, ignored
The analysis reason
After modifying the nginx configuration parameters, use nginx -t to check the configuration. Nginx -s reload nginx -s reload nginx -s reload [WARN] Conflicting server name “localhost” on 0.0.0.0:8088, ignored means that the server name is repeatedly bound, but this warning does not affect server operation. Also, this duplicate binding means a duplicate between the nginx service running now and the new configuration that will be loaded, so this warning is not really necessary.
The solution
Find the nginx. Conf or/usr/local/nginx/conf/conf. D server_name followed in the domain name, have appeared in other server block to avoid the same domain name can’t point to multiple directories If you have repeated domain configuration, change, Save and restart nginx
2. There is only one port number 80 by default, and no other port number is applied. How should multiple projects be distinguished?
The solution
This can be distinguished by the project name. Use it with the project name on the access link.
Take a chestnut
The root path is /var/project, the subproject is A /projectA, so the full path is /var/project/projecta. The access path is the domain name plus /projectA, and the front-end route is followed.
3. Use a different port number, why does not take effect?
If your server is in Ali cloud, then the port number is also required to apply for configuration above, equivalent to open the whitelist.
Small blessing
2020.10.24
Wish you no bug, tonight do not work overtime; May you not bald also become strong; Those who are single find partners, those who have partners do not cheat.
❤️ encourage
Thank you for reading this article and please give it a thumbs up if you find it useful. Your likes are the biggest support for me. If you have any questions, please feel free to comment.