1.HTTP服務器
Nginx是一個HTTP服務器,可以將服務器上的靜態文件(如HTML、圖片)通過HTTP協議展現給客戶端。
2.反向代理服務器
Nginx也是反向代理服務器。
說反向代理之前先說一下正向代理,正向代理相信很多大陸同胞都在這片神奇的土地上用過了。就是訪問國外網被牆了,然后找個代理服務,通過該服務器訪問國外網站,這個是正向代理。
反向代理是 客戶端訪問代理服務器,但是代理服務器沒有用戶需要的資源,然后代理服務器偷偷訪問應用服務器,獲取資源返回給用戶,用戶不知道代理服務器是訪問了應用服務器,代理服務器也隱藏了應用服務器的url。(反向代理的典型用途是將 防火牆后面的服務器提供給Internet用戶訪問)
3.負載均衡
Nginx可以通過反向代理來實現負載均衡。
二、Nginx 安裝
2.1 CentOS 7 安裝 Nginx
2.1.1.添加Nginx到YUM源
添加CentOS 7 Nginx yum資源庫,打開終端,使用以下命令:
sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
2.2.2.安裝Nginx
在你的CentOS 7 服務器中使用yum命令從Nginx源服務器中獲取來安裝Nginx:
sudo yum install -y nginx
Nginx將完成安裝在你的CentOS 7 服務器中。
2.2.3. Nginx啟動
nginx -c nginx.conf
啟動后就可以訪問 域名或者本機IP 如果出現下圖,說明啟動成功。
2.2.4. Nginx 停止
//查詢出nginx的pid ps -ef|grep nginx //通過kill 命令殺死 nginx kill pid
注意圖中nginx的 pid 為 10497
2.2.5. Nginx配置信息
網站文件存放默認目錄
/usr/share/nginx/html
網站默認站點配置
/etc/nginx/conf.d/default.conf
自定義Nginx站點配置文件存放目錄
/etc/nginx/conf.d/
Nginx全局配置
/etc/nginx/nginx.conf
2.2 docker 安裝 Nginx
2.2.1下載鏡像,
docker pull nginx:1.9
2.2.2 啟動容器,
docker run -d -p 8080:80 nginx:1.9
把容器內的nginx的80端口,映射到當前服務器的8080端口,假設當前服務器的ip是192.168.0.100,瀏覽器輸入http://192.168.0.100:8080/,就可以看到nginx已啟動,
三、Nginx 使用
關於靜態資源服務器的使用這里就不做說明了。
3.1 反向代理
小例子:實現訪問本機 ip 然后代理 我的博地址 ,也就是在瀏覽器輸入本機地址,然后跳轉到我的博客。
3.1.1 本地安裝nginx
如果是本地安裝的nginx 的話需要修改 /etc/nginx/conf.d/default.conf 配置文件
注釋原有的 location 然后替換為新的location
//注釋原有的 # location / { # root /usr/share/nginx/html; # index index.html index.htm; # }
//新添加的
location / {
proxy_pass http://blog.csdn.net/u012373815?viewmode=list;
}
然后重啟nginx ,訪問本機ip 就會代理到“http://blog.csdn.net/u012373815?viewmode=list“ 我的博客地址。
3.1.2 Docker 安裝
如果是docker 安裝的話,需要本地隨意目錄下新建default.conf 文件,內容如下:
server {
listen 80; server_name localhost; <span class="hljs-variable"><span class="hljs-comment">#charset koi8</span><span class="hljs-attribute"><span class="hljs-comment">-r;</span>
<span class="hljs-variable"><span class="hljs-comment">#access_log /</span><span class="hljs-built_in"><span class="hljs-comment">var/</span><span class="hljs-keyword"><span class="hljs-comment">log/nginx/</span><span class="hljs-keyword"><span class="hljs-comment">log/host</span><span class="hljs-built_in"><span class="hljs-comment">.access</span><span class="hljs-built_in"><span class="hljs-comment">.</span><span class="hljs-keyword"><span class="hljs-comment">log main;</span>
<span class="hljs-attribute">location</span> <span class="hljs-subst">/ {
<span class="hljs-attribute">proxy_pass</span> http:<span class="hljs-comment">//blog.csdn.net/u012373815?viewmode=list;
}
<span class="hljs-attribute">error_page</span> <span class="hljs-number"><span class="hljs-number">500</span> <span class="hljs-number"><span class="hljs-number">502</span> <span class="hljs-number"><span class="hljs-number">503</span> <span class="hljs-number"><span class="hljs-number">504</span> /<span class="hljs-number">50x<span class="hljs-built_in">.html;
<span class="hljs-attribute">location</span> <span class="hljs-subst">= /<span class="hljs-number">50x<span class="hljs-built_in">.html {
<span class="hljs-attribute">root</span> /usr/share/nginx/html;
}
}
然后重新啟動容器,將default.conf 文件映射到docker容器內。
啟動命令如下:
docker run -p 8080:80 --name myNginx -v /本地路徑/default.conf:/etc/nginx/conf.d/default.conf -d nginx:1.9
容器啟動后 ,訪問本機ip 就會代理到“http://blog.csdn.net/u012373815?viewmode=list“ 我的博客地址。
3.2 負載均衡
上面的配置只實現了反向代理沒有實現傳說中的負載均衡。所有的請求就都被反向代理到 我的博客地址去了。這樣我們反向代理的功能是實現了,可是就能代理到一台服務器上哪有什么負載均衡呀?這就要用到 nginx 的 upstream 模塊了。
upstream backend {
ip_hash; server backend1.example.com; server backend2.example.com; server backend3.example.com; server backend4.example.com; } location / { proxy_pass http://backend; }
我們在 upstream 中指定了一組機器,並將這個組命名為 backend,這樣在 proxypass 中只要將請求轉移到 backend 這個 upstream 中我們就實現了在四台機器的反向代理加負載均衡。其中的 iphash指明了我們均衡的方式是按照用戶的 ip 地址進行分配。
要讓配置生效,我們不必重啟 nginx 只需要 reload 配置即可。
負載均衡配置示例
假設這樣一個應用場景:將應用部署在 192.168.1.11:80、192.168.1.12:80、192.168.1.13:80 三台 linux 環境的服務器上。網站域名叫 www.helloworld.com,公網 IP 為 192.168.1.11。在公網 IP 所在的服務器上部署 nginx,對所有請求做負載均衡處理。
nginx.conf 配置如下:
http { #設定mime類型,類型由mime.type文件定義 include /etc/nginx/mime.types; default_type application/octet-stream; #設定日志格式 access_log /var/log/nginx/access.log; <span class="hljs-comment"><span class="hljs-comment">#設定負載均衡的服務器列表</span>
<span class="hljs-attribute"><span class="hljs-attribute">upstream</span> load_balance_server {
<span class="hljs-comment"><span class="hljs-comment">#weigth參數表示權值,權值越高被分配到的幾率越大</span>
<span class="hljs-attribute"><span class="hljs-attribute">server</span> <span class="hljs-number"><span class="hljs-number">192.168.1.11:80</span> weight=<span class="hljs-number"><span class="hljs-number">5</span>;
<span class="hljs-attribute"><span class="hljs-attribute">server</span> <span class="hljs-number"><span class="hljs-number">192.168.1.12:80</span> weight=<span class="hljs-number"><span class="hljs-number">1</span>;
<span class="hljs-attribute"><span class="hljs-attribute">server</span> <span class="hljs-number"><span class="hljs-number">192.168.1.13:80</span> weight=<span class="hljs-number"><span class="hljs-number">6</span>;
}
#HTTP服務器
server {
#偵聽80端口
listen 80;
<span class="hljs-comment"><span class="hljs-comment">#定義使用www.xx.com訪問</span>
<span class="hljs-attribute"><span class="hljs-attribute">server_name</span> www.helloworld.com;
<span class="hljs-comment"><span class="hljs-comment">#對所有請求進行負載均衡請求</span>
<span class="hljs-attribute"><span class="hljs-attribute">location</span> / {
<span class="hljs-attribute"><span class="hljs-attribute">root</span> /root; <span class="hljs-comment"><span class="hljs-comment">#定義服務器的默認網站根目錄位置</span>
<span class="hljs-attribute"><span class="hljs-attribute">index</span> index.html index.htm; <span class="hljs-comment"><span class="hljs-comment">#定義首頁索引文件的名稱</span>
<span class="hljs-attribute"><span class="hljs-attribute">proxy_pass</span> http://load_balance_server ;<span class="hljs-comment"><span class="hljs-comment">#請求轉向load_balance_server 定義的服務器列表</span>
<span class="hljs-comment"><span class="hljs-comment">#以下是一些反向代理的配置(可選擇性配置)</span>
<span class="hljs-comment"><span class="hljs-comment">#proxy_redirect off;</span>
<span class="hljs-attribute"><span class="hljs-attribute">proxy_set_header</span> Host <span class="hljs-variable"><span class="hljs-variable">$host</span>;
<span class="hljs-attribute"><span class="hljs-attribute">proxy_set_header</span> X-Real-IP <span class="hljs-variable"><span class="hljs-variable">$remote_addr</span>;
<span class="hljs-comment"><span class="hljs-comment">#后端的Web服務器可以通過X-Forwarded-For獲取用戶真實IP</span>
<span class="hljs-attribute"><span class="hljs-attribute">proxy_set_header</span> X-Forwarded-For <span class="hljs-variable"><span class="hljs-variable">$remote_addr</span>;
<span class="hljs-attribute"><span class="hljs-attribute">proxy_connect_timeout</span> <span class="hljs-number"><span class="hljs-number">90</span>; <span class="hljs-comment"><span class="hljs-comment">#nginx跟后端服務器連接超時時間(代理連接超時)</span>
<span class="hljs-attribute"><span class="hljs-attribute">proxy_send_timeout</span> <span class="hljs-number"><span class="hljs-number">90</span>; <span class="hljs-comment"><span class="hljs-comment">#后端服務器數據回傳時間(代理發送超時)</span>
<span class="hljs-attribute"><span class="hljs-attribute">proxy_read_timeout</span> <span class="hljs-number"><span class="hljs-number">90</span>; <span class="hljs-comment"><span class="hljs-comment">#連接成功后,后端服務器響應時間(代理接收超時)</span>
<span class="hljs-attribute"><span class="hljs-attribute">proxy_buffer_size</span> <span class="hljs-number"><span class="hljs-number">4k</span>; <span class="hljs-comment"><span class="hljs-comment">#設置代理服務器(nginx)保存用戶頭信息的緩沖區大小</span>
<span class="hljs-attribute"><span class="hljs-attribute">proxy_buffers</span> <span class="hljs-number"><span class="hljs-number">4</span> <span class="hljs-number"><span class="hljs-number">32k</span>; <span class="hljs-comment"><span class="hljs-comment">#proxy_buffers緩沖區,網頁平均在32k以下的話,這樣設置</span>
<span class="hljs-attribute"><span class="hljs-attribute">proxy_busy_buffers_size</span> <span class="hljs-number"><span class="hljs-number">64k</span>; <span class="hljs-comment"><span class="hljs-comment">#高負荷下緩沖大小(proxy_buffers*2)</span>
<span class="hljs-attribute"><span class="hljs-attribute">proxy_temp_file_write_size</span> <span class="hljs-number"><span class="hljs-number">64k</span>; <span class="hljs-comment"><span class="hljs-comment">#設定緩存文件夾大小,大於這個值,將從upstream服務器傳</span>
<span class="hljs-attribute"><span class="hljs-attribute">client_max_body_size</span> <span class="hljs-number"><span class="hljs-number">10m</span>; <span class="hljs-comment"><span class="hljs-comment">#允許客戶端請求的最大單文件字節數</span>
<span class="hljs-attribute"><span class="hljs-attribute">client_body_buffer_size</span> <span class="hljs-number"><span class="hljs-number">128k</span>; <span class="hljs-comment"><span class="hljs-comment">#緩沖區代理緩沖用戶端請求的最大字節數</span>
}
}
四、nginx配置location總結及rewrite規則寫法
4.1 正則
.
: 匹配除換行符以外的任意字符?
: 重復0次或1次+
: 重復1次或更多次*
: 重復0次或更多次\d
:匹配數字^
: 匹配字符串的開始$
: 匹配字符串的介紹{n}
: 重復n次{n,}
: 重復n次或更多次[c]
: 匹配單個字符c[a-z]
: 匹配a-z小寫字母的任意一個~ 區分大小寫匹配
~* 不區分大小寫匹配
!~和!~*分別為區分大小寫不匹配及不區分大小寫不匹配
^ 以什么開頭的匹配
$ 以什么結尾的匹配
-
小括號
()
之間匹配的內容,可以在后面通過$1
來引用,$2
表示的是前面第二個()
里的內容。正則里面容易讓人困惑的是\
轉義特殊字符。
4.2 常用變量
$args
: #這個變量等於請求行中的參數,同$query_string
$content_length
: 請求頭中的Content-length字段。$content_type
: 請求頭中的Content-Type字段。$document_root
: 當前請求在root指令中指定的值。$host
: 請求主機頭字段,否則為服務器名稱。$http_user_agent
: 客戶端agent信息$http_cookie
: 客戶端cookie信息$limit_rate
: 這個變量可以限制連接速率。$request_method
: 客戶端請求的動作,通常為GET或POST。$remote_addr
: 客戶端的IP地址。$remote_port
: 客戶端的端口。$remote_user
: 已經經過Auth Basic Module驗證的用戶名。$request_filename
: 當前請求的文件路徑,由root或alias指令與URI請求生成。$scheme
: HTTP方法(如http,https)。$server_protocol
: 請求使用的協議,通常是HTTP/1.0或HTTP/1.1。$server_addr
: 服務器地址,在完成一次系統調用后可以確定這個值。$server_name
: 服務器名稱。$server_port
: 請求到達服務器的端口號。$request_uri
: 包含請求參數的原始URI,不包含主機名,如:”/foo/bar.php?arg=baz”。$uri
: 不帶請求參數的當前URI,$uri不包含主機名,如”/foo/bar.html”。$document_uri
: 與$uri相同。
4.3 flag標志位
last
: 相當於Apache的[L]標記,表示完成rewritebreak
: 停止執行當前虛擬主機的后續rewrite指令集redirect
: 返回302臨時重定向,地址欄會顯示跳轉后的地址permanent
: 返回301永久重定向,地址欄會顯示跳轉后的地址
#路徑重寫配置Demo location /demo/test/ { #以/demo/test路徑開始 #原始路徑: http://127.0.0.1:8080/demo/test/1.html 重寫后:http://127.0.0.1:8888/demo/test2/1.html rewrite ^/demo/test/(.*)$ http://127.0.0.1:8888/demo/test2/$1 break; <span class="hljs-comment"><span class="hljs-comment">#以html文件結尾</span>
<span class="hljs-comment"><span class="hljs-comment">#原始路徑: http://127.0.0.1:8080/demo/test/1.html 重寫后:http://127.0.0.1:8888/test/demo/test/1.html</span>
<span class="hljs-comment"><span class="hljs-comment"># 此種方式配置會改變瀏覽器地址,引發跨域請求的問題</span>
<span class="hljs-comment"><span class="hljs-comment">#rewrite ^/(.*\.html)$ http://127.0.0.1:8888/test/$1 break;</span>
<span class="hljs-comment"><span class="hljs-comment"># 此種方式,結合proxy_pass 重寫后則不會改變瀏覽器地址,不存在跨域問題</span>
<span class="hljs-comment"><span class="hljs-comment">#rewrite ^/(.*\.html)$ /test/$1 break;</span>
<span class="hljs-comment"><span class="hljs-comment">#proxy_pass http://127.0.0.1:8888;</span>