Nginx application scenarios, Nginx virtual host configuration tutorial, Nginx reverse proxy, nginx Virtual Host
I. Nginx application scenarios
1. http server. Nginx is an http service that provides http services independently. Web static servers can be used.
2. VM. Multiple websites can be virtualized on one server. For example, the virtual host used by a personal website.
3. reverse proxy and Server Load balancer. When a website's access volume reaches a certain level and a single server cannot meet users' requests, you need to use multiple Server clusters to use nginx for reverse proxy. In addition, multiple servers can carry the load evenly, so that a server is not idle due to the high load of a server.
Ii. Configure the virtual host in Nginx
(1) differentiate virtual machines through ports
Nginx configuration file:
/Usr/local/nginx/conf/nginx. conf
# 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;
# Here is the focus. You can configure multiple server nodes and set different ports to access different servers.
Server {
Listen 80;
Server_name localhost;
# Charset koi8-r;
# Access_log logs/host. access. log main;
Location /{
Root html;
Index index.html index.htm;
}
}
Server {
Listen 81;
Server_name localhost;
# Charset koi8-r;
# Access_log logs/host. access. log main;
Location /{
Root html;
Index index.html index.htm;
}
}
}
(2) differentiate virtual hosts by domain names
The focus is still to configure the server node, but the port here remains unchanged, all are port 80, while server_name is different domain names
Server {
Listen 80;
Server_name e3mall. xin;
# Charset koi8-r;
# Access_log logs/host. access. log main;
Location /{
Root html-taobao;
Index index.html index.htm;
}
}
Server {
Listen 80;
Server_name e3bos. top;
# Charset koi8-r;
# Access_log logs/host. access. log main;
Location /{
Root html-baidu;
Index index.html index.htm;
}
}
}
Iii. implement reverse proxy for Nginx
The two domain names direct to the same nginx server. When users access different domain names, different webpage content is displayed.
Nginx configuration file
Upstream extends AT1 {
Server 127.0.0.1: 8080;
}
Server {
Listen 80;
Server_name e3bos, top;
# Charset koi8-r;
# Access_log logs/host. access. log main;
Location /{
Proxy_pass http: // extends AT1;
Index index.html index.htm;
}
}
Upstream tomcat2 {
Server 127.0.0.1: 8081;
}
Server {
Listen 80;
Server_name e3mall. xin;
# Charset koi8-r;
# Access_log logs/host. access. log main;
Location /{
Proxy_pass http: // tomcat2;
Index index.html index.htm;
}
}