Recently work often use nginx, in order to better use Nginx, I collected a lot of nginx related technical articles to read, so only the following content. Here, I would like to express our thanks for the articles quoted and referenced in this article. If the relevant content of the text is wrong, please also welcome a message to put forward.
Installation and startup of Nginx
1) Pcre installation, support regular expression
http://www.pcre.org/
# tar ZXVF pcre-7.9.tar.gz
# CD pcre-7.9
#./configure
# Make && make install
2) OpenSSL installation (optional), sites that support security protocols
http://www.openssl.org/
# tar ZXVF openssl-0.9.8l.tar.gz
# CD OPENSSL-0.9.8L
#./config
# Make && make install
3) Installation of Nginx
# tar ZXVF nginx-0.7.64.tar.gz
# CD nginx-0.7.64
4) Start
# CD Usr/local/nginx/sbin
#/nginx
5) Restart
./nginx-s Reload
NGINX.CONF Basic Configuration
#user nobody;
Worker_processes 1; #nginx进程数, the recommended setting is equal to the total CPU core number.
Events {
Worker_connections 1024; #单个进程最大连接数 (maximum number of connections = number of connections * processes)
}
HTTP {
Include Mime.types; #文件扩展名与文件类型映射表
Default_type Application/octet-stream; #默认文件类型
Sendfile on; #sendfile on; #开启高效文件传输模式
keepalive_timeout; #长连接超时时间, Unit is seconds
#gzip模块设置 (slightly)
Upstream blog.ha97.com {
#upstream的负载均衡, weight is a weight that can be defined according to machine configuration
#weigth参数表示权值, the higher the weight, the greater the probability of being allocated.
Server 192.168.80.121:80 weight=3;
Server 192.168.80.122:80 weight=2;
Server 192.168.80.123:80 weight=3;
}
server {
Listen 80;
server_name localhost;
Location/{
Proxy_pass Http://192.168.24.152:8080/test1;
Proxy_set_header X-real-ip $remote _addr;
Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for;
}
location/test {
Proxy_pass http://192.168.24.152:8080/abc;
Proxy_set_header X-real-ip $remote _addr;
Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for;
}
}
server{
Listen 8082; #监听端口
server_name localhost;
Location/{
Proxy_pass Http://192.168.24.152:8080/test1;
Proxy_set_header X-real-ip $remote _addr;
Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for;
}
}
}
nginx.conf Server Configuration Instructions
1) Server Configuration instructions
server {
Listen 80; #监听端口
server_name localhost; #域名可以有多个, separated by a space
Location/{
Proxy_pass Http://192.168.24.152:8080/test1;
Proxy_set_header X-real-ip $remote _addr;
#把真实的IP发送给 the forwarded Web server, otherwise the forwarded IP is the Nginx IP
Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for;
#意思是增加一个 $proxy _add_x_forwarded_for to the x-forwarded-for.
#例X-forwarded-for:1.1.1.1, 2.2.2.2, 3.3.3.3 The request was sent by 1.1.1.1,
#经过三层代理, #第一层是2.2.2.2, the second tier is 3.3.3.3, and the source of this request
#IP4.4.4.4 is the third tier agent
}
}
2) Multiple server configurations, monitoring different ports
server {#server1
Listen 80;
server_name localhost;
Location/{
Proxy_pass Http://192.168.24.152:8080/test1;
Proxy_set_header X-real-ip $remote _addr;
Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for;
}
}
server{#server2
Listen 8082; #监听端口
server_name localhost;
Location/{
Proxy_pass Http://192.168.24.152:8080/test1;
Proxy_set_header X-real-ip $remote _addr;
Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for;
}
}
Server configuration path matching problem
The matching rules that may be involved in Nginx location are:
= Exact match
^~ normal character matching, case-sensitive
~ Regular match, case sensitive
/xxx/yyy.zzz longest match
/
The order of precedence is as follows:
= priority is greater than ^~
^~ priority is greater than ~
~ Contrast/xxx/yyy.zzz
^~ priority is greater than/xxx/yyy.zzz
-Priority greater than/
/XXX/YYY.ZZZ priority is greater than/
/xxx/yyy/priority is greater than/xxx/
In summary the location rule priority order is = > ^~ > ~ >/xxx/yyy.zzz >/, the priority of path matching is independent of the position in the document.
Nginx Implements SSL Reverse proxy
server {
Listen 443; #监听443端口
server_name localhost;
SSL on; #启用ssl加密
SSL_CERTIFICATE/ECT/CERT/FREE4LAB.CRT; #服务器证书crt文件
Ssl_certificate_key/ect/cert/free4lab.key; #服务器私钥key文件
Location/{
Proxy_pass Http://192.168.24.152:8080/test1;
Proxy_set_header X-real-ip $remote _addr;
Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for;
}
}
Turn from:
Actual Combat | Talk to me today. Nginx Reverse proxy Usage-today's headline (toutiao.com)
Http://toutiao.com/a6309953445340594433/?tt_from=mobile_qq&utm_campaign=client_share&app=news_article &utm_source=mobile_qq&iid=4913332425&utm_medium=toutiao_ios
Nginx Reverse proxy use "reprint"