<center>百萬(wàn)架構(gòu)師系列文章閱讀體驗(yàn)感更佳</center>
<center>原文鏈接:https://javaguide.net</center>
Nginx 的初步認(rèn)識(shí)及配置
課程目標(biāo)
- Nginx 在分布式架構(gòu)中的應(yīng)用分析
- 常用的 Web 服務(wù)器及差異
- Nginx 的安裝以及配置分析
- Nginx 虛擬主機(jī)配置
- 詳解 Location 的匹配規(guī)則
背景
早期用 F5 做負(fù)載均衡

后來(lái)通過(guò)負(fù)載均衡和熱備來(lái)提高整個(gè)的 QPS。

什么是 Nginx
Nginx 是一個(gè)高性能的反向代理服務(wù)器
- 正向代理代理的是客戶端
- 反向代理代理的是服務(wù)端
Apache、Tomcat、Nginx
都是 HTTP 服務(wù)器,他們都會(huì)放到我們服務(wù)端的服務(wù)器上,通過(guò)綁定 IP 和 端口的方式,去監(jiān)聽(tīng)一個(gè)端口號(hào)。接收客戶端的 HTTP 請(qǐng)求。

展示給用戶端。
JSP 是動(dòng)態(tài)資源。
| apache、nginx | 靜態(tài)文件服務(wù)器 | css / js |
| Tomcat | 動(dòng)態(tài)文件服務(wù)器 | jsp / servlet |
| apache | 有一些性能瓶頸的 |
| Nginx | 主要是用來(lái)處理高并發(fā)的請(qǐng)求 |
Nginx 和 Apache 也是可以做動(dòng)態(tài)解析的,但是它本身不是做這個(gè)的。
Nginx 功能
- 反向代理。
- 流量分發(fā)
- 動(dòng)靜分離
- 認(rèn)證
- 授權(quán)
它還是一個(gè)高度模塊化的設(shè)計(jì)
安裝 Nginx
[root@Darian1 software]# mkdir nginx [root@Darian1 software]# cd nginx-1.1.4/ [root@Darian1 nginx-1.1.4]# ./configure --prefix=/software/nginx ./configure: error: the HTTP rewrite module requires the PCRE library. You can either disable the module by using --without-http_rewrite_module option, or install the PCRE library into the system, or build the PCRE library statically from the source with nginx by using --with-pcre=<path> option. [root@Darian1 nginx-1.1.4]# yum install pcre-devel [root@Darian1 nginx-1.1.4]# ./configure --prefix=/software/nginx ./configure: error: the HTTP gzip module requires the zlib library. You can either disable the module by using --without-http_gzip_module option, or install the zlib library into the system, or build the zlib library statically from the source with nginx by using --with-zlib=<path> option. [root@Darian3 nginx-1.4.1]# yum install -y zlib-devel [root@Darian1 nginx-1.1.4]# ./configure --prefix=/software/nginx [root@Darian1 nginx-1.1.4]# make && make install [root@Darian1 software]# cd nginx [root@Darian1 nginx]# ll 總用量 4 drwxr-xr-x. 2 root root 4096 1月 17 13:58 conf drwxr-xr-x. 2 root root 68 1月 17 13:58 html drwxr-xr-x. 2 root root 6 1月 17 13:58 logs drwxr-xr-x. 2 root root 36 1月 17 13:59 sbin [root@Darian1 nginx]# cd sbin/ [root@Darian1 sbin]# ll 總用量 1704 -rwxr-xr-x. 1 root root 868424 1月 17 13:59 nginx -rwxr-xr-x. 1 root root 868424 1月 17 13:58 nginx.old [root@Darian1 sbin]# ./nginx [root@Darian1 sbin]# ./nginx -s stop
- 下載 tar 包
tar -zxvf nginx.tar.gz./configure [--prefix]make && make install
啟動(dòng)和停止
./nginx./nginx -s stop

nginx.conf
[root@Darian1 nginx]# cd conf/
[root@Darian1 conf]# ll
總用量 60
-rw-r--r--. 1 root root 979 1月 17 13:58 fastcgi.conf
-rw-r--r--. 1 root root 979 1月 17 13:59 fastcgi.conf.default
-rw-r--r--. 1 root root 909 1月 17 13:58 fastcgi_params
-rw-r--r--. 1 root root 909 1月 17 13:59 fastcgi_params.default
-rw-r--r--. 1 root root 2837 1月 17 13:59 koi-utf
-rw-r--r--. 1 root root 2223 1月 17 13:59 koi-win
-rw-r--r--. 1 root root 3268 1月 17 13:58 mime.types
-rw-r--r--. 1 root root 3268 1月 17 13:59 mime.types.default
-rw-r--r--. 1 root root 2685 1月 17 13:58 nginx.conf
-rw-r--r--. 1 root root 2685 1月 17 13:59 nginx.conf.default
-rw-r--r--. 1 root root 544 1月 17 13:58 scgi_params
-rw-r--r--. 1 root root 544 1月 17 13:59 scgi_params.default
-rw-r--r--. 1 root root 570 1月 17 13:58 uwsgi_params
-rw-r--r--. 1 root root 570 1月 17 13:59 uwsgi_params.default
-rw-r--r--. 1 root root 3610 1月 17 13:59 win-utf
[root@Darian1 conf]# vim 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 {
# 允許連接的最大數(shù)量
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;
# server_name localhost;
# ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_timeout 5m;
# ssl_protocols SSLv2 SSLv3 TLSv1;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
Main
event
http
虛擬主機(jī)配置
針對(duì)不同的端口號(hào)做不同的訪問(wèn),不同域名做不同的訪問(wèn)。
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
}
基于ip的虛擬主機(jī)
不演示
基于端口號(hào)的虛擬主機(jī)
server {
listen 8080;
server_name localhost;
location / {
root html;
index index.html;
}
}
我們可以配置一個(gè)端口段,去做不同的路由轉(zhuǎn)發(fā)。
基于域名的虛擬主機(jī)
購(gòu)買(mǎi)主域名以后,可以基于主域名開(kāi)放很多二級(jí)域名。
www.darian.com / ask.darian.com / git.darian.com / bbs.darian.com
server{
listen 8080;
server_name localhost;
location / {
root html;
index index.html;
}
}
server{
listen 80;
server_name bbs.darian.com;
location / {
root html;
index bbs.html;
}
}
server{
listen 80;
server_name ask.darian.com;
location / {
root html;
index ask.html;
}
}
server{
listen 80;
server_name www.darian.com;
location / {
# 去這個(gè)目錄下邊找
root html;
index index.html;
}
}
[root@Darian1 nginx]# ./sbin/nginx
[root@Darian1 nginx]# ./sbin/nginx -s reload
[root@Darian1 nginx]# ps -ef|grep nginx
root 9595 1 0 15:16 ? 00:00:00 nginx: master process ./sbin/nginx
nobody 9596 9595 0 15:16 ? 00:00:00 nginx: worker process
root 9619 9550 0 15:28 pts/2 00:00:00 grep --color=auto nginx
需要去 html 目錄下創(chuàng)建幾個(gè)文件
[root@Darian1 html]# ll
總用量 24
-rw-r--r--. 1 root root 383 1月 17 13:58 50x.html
-rw-r--r--. 1 root root 26 1月 17 16:13 ask.html
-rw-r--r--. 1 root root 25 1月 17 16:14 bbs.html
-rw-r--r--. 1 root root 151 1月 17 13:58 index.html
-rw-r--r--. 1 root root 7133 1月 17 13:58 ngx_core_module.html
可能需要刷新 DNS




用本地 host 實(shí)現(xiàn)了域名的解析。
二級(jí)域名隨意創(chuàng)造,備案主域名就好了
建議
將配置單獨(dú)抽出來(lái),放到一個(gè)地方去統(tǒng)一地進(jìn)行管理。
代理解決的事情
- 負(fù)載均衡
- 緩存
- 限流
- 內(nèi)外網(wǎng)的隔離,做安全性的東西
location
nginx 里邊,需要知道 `location` ,對(duì) `location` 做一些請(qǐng)求的轉(zhuǎn)發(fā)。
根據(jù)請(qǐng)求的 URI 匹配到對(duì)應(yīng)的 location 以后,去做對(duì)應(yīng)的轉(zhuǎn)發(fā)。
配置語(yǔ)法
location [= | ~* | ^~ ] /uri/ {...}
配置規(guī)則
location = /uri 精準(zhǔn)匹配
location ^~ /uri 前綴匹配
location ~ /uri
location / 通用匹配
規(guī)則的優(yōu)先級(jí)
1 location = /
2 location = /index
3 location ^~ /article/
4 location ^~ /article/files/
5 location ~ \.(gif|png|js|css)$
6 location /
http://192.168.11.154/ -> 1
http://192.168.11.154/index ->2
http://192.168.11.154/article/files/1.txt ->4
http://192.168.11.154/darian.png ->5
(靜態(tài)資源服務(wù)器,可以做壓縮的)
匹配規(guī)則是不能重復(fù)的。
- 精準(zhǔn)匹配是優(yōu)先級(jí)最高
- 普通匹配(最長(zhǎng)的匹配)
- 正則匹配
實(shí)際使用建議
# 根目錄的配置規(guī)則,首頁(yè)是靜態(tài)頁(yè)面
location =/ {
}
# 通用匹配 http://192.168.40.128:8080/index.html
location / {
}
# 靜態(tài)規(guī)則,做動(dòng)靜分離
location ~* \.(gif|....)${
}
Nginx模塊
Nginx 內(nèi)部都是由一些核心和非核心的第三方模塊組成的。它的模塊化,我們可以做一些動(dòng)態(tài)的集成和擴(kuò)展。
除了它本身的功能以外,我們還可以利用第三方的配置去實(shí)現(xiàn)更復(fù)雜的功能。
默認(rèn)的:
- 反向代理
- nginx core
模塊分類(lèi)
- 核心模塊 ngx_http_core_module
- 標(biāo)準(zhǔn)模塊 http模塊
- 第三方模塊
可以把 模塊看成插件
ngx_http_core_module
server{
listen port
server_name ...
localtion{
root ...
}
}
Location 實(shí)現(xiàn) URI 到文件系統(tǒng)路徑的映射
-
error_page
# redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; }
ngx_http_access_module
實(shí)現(xiàn)基于 IP 的訪問(wèn)控制功能
allow address | CIDR | unix: | all;deny address | CIDR | unix: | all;

自上而下檢查,一旦匹配,將生效,條件嚴(yán)格的置前
可以用 -t 來(lái)看語(yǔ)法有沒(méi)有問(wèn)題
[root@Darian1 sbin]# ./nginx -t
如何添加第三方模塊
Nginx 不支持動(dòng)態(tài)去安裝或者加載模塊。安裝別的代碼的時(shí)候需要重新編譯。(我們很少去編寫(xiě)模塊)
- 原來(lái)所安裝的配置,你必須在重新安裝新模塊的時(shí)候,加上
- 不能直接
make install
configure --prefix=/data/program/nginx
安裝方法
./configure --prefix=/安裝目錄 --add-module = /第三方模塊的目錄
./configure --prefix=/data/program/nginx --with-http_stub_status_module --with-http_random_index_module
cp objs/nginx $nginx_home/sbin/nginx
[root@Darian1 nginx-1.1.4]# cd ../nginx-1.1.4/ [root@Darian1 nginx-1.1.4]# ./configure --prefix=/software/nginx --with-http_stub_status_module --with-http_random_index_module [root@Darian1 nginx-1.1.4]# ps -ef|grep nginx root 9595 1 0 15:16 ? 00:00:00 nginx: master process ./sbin/nginx nobody 9755 9595 0 16:25 ? 00:00:00 nginx: worker process root 13696 9550 0 19:35 pts/2 00:00:00 grep --color=auto nginx [root@Darian1 nginx-1.1.4]# kill -9 9595 [root@Darian1 nginx-1.1.4]# kill -9 9755 [root@Darian1 nginx-1.1.4]# ps -ef|grep nginx root 13699 9550 0 19:35 pts/2 00:00:00 grep --color=auto nginx [root@Darian1 nginx-1.1.4]# cp objs/nginx ../nginx/sbin/nginx cp:是否覆蓋"../nginx/sbin/nginx"? y [root@Darian1 nginx-1.1.4]#如果直接
make && make install會(huì)直接就沒(méi)了。所以 替換。
[root@Darian1 nginx-1.1.4]# kill -9 13713 [root@Darian1 nginx-1.1.4]# ./configure --prefix=/software/nginx --with-http_stub_status_module --with-http_random_index_module [root@Darian1 nginx-1.1.4]# make [root@Darian1 nginx-1.1.4]# cp objs/nginx ../nginx/sbin/nginx cp:是否覆蓋"../nginx/sbin/nginx"? y
http_stub_status_module
location /status{
stub_status on;
}

- Active connections:當(dāng)前狀態(tài),活動(dòng)狀態(tài)的連接數(shù)
- accepts:統(tǒng)計(jì)總值,已經(jīng)接收的客戶端請(qǐng)求的總數(shù)
- handled:統(tǒng)計(jì)總值,已經(jīng)處理完成的客戶端請(qǐng)求的總數(shù)
- requests:統(tǒng)計(jì)總值,客戶端發(fā)來(lái)的總的請(qǐng)求數(shù)
- Reading:當(dāng)前狀態(tài),正在讀取客戶端請(qǐng)求報(bào)文首部的連接的連接數(shù)
- Writing:當(dāng)前狀態(tài),正在向客戶端發(fā)送響應(yīng)報(bào)文過(guò)程中的連接數(shù)
- Waiting:當(dāng)前狀態(tài),正在等待客戶端發(fā)出請(qǐng)求的空閑連接數(shù)
http_random_index_module
隨機(jī)顯示主頁(yè)
一般情況下,一個(gè)站點(diǎn)默認(rèn)首頁(yè)都是定義好的 `index.html` 、 `index.shtml` 等等,如果站點(diǎn)下有很多頁(yè)面想隨機(jī)展示給用戶瀏覽,那得程序上實(shí)現(xiàn),很麻煩,使用 `nginx` 的 `random index` 即可簡(jiǎn)單實(shí)現(xiàn)這個(gè)功能,凡是以/結(jié)尾的請(qǐng)求,都會(huì)隨機(jī)展示當(dāng)前目錄下的文件作為首頁(yè)
-
添加
random_index on配置,默認(rèn)是關(guān)閉的location / { root html; random_index on; index index.html index.htm; } 在 html 目錄下創(chuàng)建多個(gè) html 頁(yè)面
他就會(huì)多個(gè) HTML 頁(yè)面隨機(jī)地展示在首頁(yè)。
( 官方的模塊,你可以直接通過(guò) with model 去集成,如果是下載第三方插件可以通過(guò) http model 去集成。 )
<center>百萬(wàn)架構(gòu)師系列文章閱讀體驗(yàn)感更佳</center>
<center>原文鏈接:https://javaguide.net</center>
來(lái)源于: https://javaguide.net