2一、Nginx 常見問題

1.多個server_name容易產生衝突,會按照以下順序匹配

  • 1.首先選擇全部的字符串徹底匹配的server_name。(徹底匹配)
  • 2.選擇通配符在前面的server_name,如*.bgx.com www.bgx.com
  • 3.選擇通配符在後面的server_name,如bgx.* bgx.com bgx.cn
  • 4.最後選擇使用正則表達式匹配的server_name
  • 5.若是所有都沒有匹配到,那麼將選擇在listen配置項後加入[default_server]的server塊
  • 6.若是沒寫,那麼就找到匹配listen端口的第一個Server塊的配置文件

Nginx多個相同Server_name優先級php

1.1.環境準備

[root@nginx ~]# mkdir /soft/code{1..3} -p
[root@nginx ~]# for i in {1..3};do echo "<h1>Code $i</h1>" > /soft/code"$i"/index.html;done

1.2.準備多份相同Nginx配置文件

[root@Nginx conf.d]# ll
總用量 12
-rw-r--r-- 1 root root 123 4月  19 19:08 testserver1.conf
-rw-r--r-- 1 root root 123 4月  19 19:09 testserver2.conf
-rw-r--r-- 1 root root 123 4月  19 19:09 testserver3.conf

//內容以下
[root@Nginx conf.d]# cat testserver{1..3}.conf
server {
        listen 80;
        server_name testserver1 192.168.69.113;

        location / {
                root /soft/code1;
                index index.html;
        }
}
server {
        listen 80;
        server_name testserver2 192.168.69.113;

        location / {
                root /soft/code2;
                index index.html;
        }
}
server {
        listen 80;
        server_name testserver3 192.168.69.113;

        location / {
                root /soft/code3;
                index index.html;
        }
}

//檢測語法
[root@Nginx conf.d]# nginx -t
nginx: [warn] conflicting server name "192.168.69.113" on 0.0.0.0:80, ignored
nginx: [warn] conflicting server name "192.168.69.113" on 0.0.0.0:80, ignored
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

//重啓Nginx
[root@Nginx conf.d]# nginx -t

1.3.測試訪問效果

#1.當用戶第一次訪問, 由code1.conf返回輸出信息
[root@Nginx conf.d]# curl 192.168.69.113
<h1>Code 1</h1>

#2.此時將code1.conf修改成code5.conf後進行重載Nginx
[root@Nginx conf.d]# mv testserver1.conf testserver5.conf
[root@Nginx conf.d]# nginx -s reload

#3.再次訪問時, 由code2.conf返回輸出信息
[root@Nginx conf.d]# curl 192.168.69.113
<h1>Code 2</h1>

2.如何經過default_server 禁止用戶經過IP地址訪問,或使用default_server進行導流

2.1禁止直接經過IP訪問

[root@web01 conf.d]# cat server4.conf 
server {
	listen 80 default_server;
	server_name _;
	return 503;
}

2.2導流:將流量導入本身的網站

[root@web01 conf.d]# cat server4.conf 
server {
	listen 80 default_server;
	server_name _;
	return 302 https://www.xuliangwei.com;
}

3.include的做用:

一臺服務器配置多個網站,若是配置都寫在nginx.conf主配置文件中,會致使nginx.conf主配置文件變得很是龐大並且可讀性很是的差。那麼後期的維護就變得麻煩。 假設如今但願快速的關閉一個站點,該怎麼辦?html

  • 1.若是是寫在nginx.conf中,則須要手動註釋,比較麻煩
  • 2.若是是include的方式,那麼僅需修改配置文件的擴展名,便可完成註釋

Include包含的做用是爲了簡化主配置文件,便於人類可讀。java

inlcude /etc/nginx/online/*.conf #線上使用的配置 /etc/nginx/offline #保留配置,不啓用(下次使用在移動到online中)nginx

4.nginx路徑root與alias:

root與alias路徑匹配主要區別在於nginx如何解釋location後面的uri,這會使二者分別以不一樣的方式將請求映射到服務器文件上,alias是一個目錄別名的定義,root則是最上層目錄的定義。 root的處理結果是:root路徑+location路徑 alias的處理結果是:使用alias定義的路徑web

4.1root路徑配置

[root@Nginx ~]# mkdir /local_path/code/request_path/code/ -p
[root@Nginx ~]# echo "Root" > /local_path/code/request_path/code/index.html

//Nginx的root配置
[root@Nginx ~]# cat /etc/nginx/conf.d/root.conf 
server {
        listen 80;
        index index.html;
        location /request_path/code/ {
                root /local_path/code/;
        }
}

//請求測試
[root@Nginx conf.d]# curl http://192.168.69.113/request_path/code/index.html
Root

//實際請求本地文件路徑爲
/local_path/code/'request_path/code'/index.html

4.2alias路徑配置

[root@Nginx ~]# mkdir /local_path/code/request_path/code/ -p
[root@Nginx ~]# echo "Alias" > /local_path/code/index.html

//配置文件
[root@Nginx ~]# cat /etc/nginx/conf.d/alias.conf 
server {
        listen 80;
        index index.html;
        location /request_path/code/ {
                alias /local_path/code/;
        }
}

//測試訪問
[root@Nginx ~]# curl http://192.168.69.113/request_path/code/index.html
Alias

//實際訪問本地路徑
/local_path/code/'index.html'

4.3現實場景會用到這種寫法:

server {
        listen 80;
        server_name image.oldboy.com;

        location / {
                root /code;
        }
		
		location ~* ^.*\.(png|jpg|gif)$ {   請求圖片回到alias定義的路徑去找
                alias /code/images/;
        }
}

5.nginx的try_file路徑匹配,按順序檢查文件是否存在

[root@bgx ~]# cat /etc/nginx/conf.d/try_file.conf
server {
    listen 80;
    server_name try.bgx.com;
    root /code;

    location / {
        try_files $uri $uri/ /404.html;
    }
}
  • 1.檢查用戶請求的uri內容是否存在本地,存在則解析
  • 2.若是請求的url不存在,則訪問對應站點目錄中的404.html文件
  • 3.最後交給index.php處理

$uri匹配文件名,如用戶請求try.oldboy.com/index.html那麼$uri則會上對應的root指定的站點目錄中查找是否存在該文件 $uri/匹配目錄下的文件,如用戶請求try.oldboy.com/,那麼$uri/則會上/對應root指定的站點目錄中查找文件面試

5.1.演示環境準備

[root@Nginx ~]# echo "Try-Page" > /soft/code/index.html
[root@Nginx ~]# echo "Tomcat-Page" > /soft/app/apache-tomcat-9.0.7/webapps/ROOT/index.html

//啓動tomcat
[root@Nginx ~]# sh /soft/app/apache-tomcat-9.0.7/bin/startup.sh
//檢查tomcat端口
[root@Nginx ~]# netstat -lntp|grep 8080
tcp6       0      0 :::8080                 :::*                    LISTEN      104952/java

5.2.當用戶訪問xx.com/index.html則由Nginx提供,若是Nginx沒有該文件則由Tomcat服務提供該文件

[root@web01 conf.d]# cat try.conf 
server {
    listen 80;
    server_name try.oldboy.com;
    root /code;
    index index.html;

    location / {
        try_files $uri $uri/  @java_page;  #:@是內部跳轉,會找location匹配
    }

	location @java_page {
		proxy_pass http://172.16.1.8:8080;
	}
}

//重啓Nginx
[root@Nginx ~]# nginx -s reload

5.3.測試tryfiles

[root@Nginx ~]# curl http://192.168.69.113/index.html
Try-Page

//將/soft/code/index.html文件移走
[root@Nginx ~]# mv /soft/code/{index.html,index.html_bak}

//發現由Tomcat吐回了請求
[root@Nginx ~]# curl http://192.168.69.113/index.html    
Tomcat-Page

6.Nginx調整上傳文件大小

在nginx使用過程當中,上傳文件的過程當中,一般須要設置nginx報文大小限制。避免出現413 Request Entity Too Large正則表達式

nginx上傳文件大小限制配置語法數據庫

Syntax:client_max_body_size size;
Default:client_max_body_size 1m;
Context:http,server,location

nginx上傳文件大小限制配置示例: 也能夠放在http層,全局生效apache

server{
...
	client_max_body_size 200m;
...
}

7.Nginx優雅顯示錯誤頁面

error_page錯誤日誌vim

[root@web01 conf.d]# cat error.conf 
server {
    listen 80;
    server_name try.oldboy.com;
    root /code;
    location / {
    	index index.html;
    }
	location ~ \.php$ {
    	fastcgi_pass 127.0.0.0:9000;
    }
    #如服務器返回以下錯誤狀態碼,則進行跳轉,跳轉至/xxx.jpg
	#error_page 403 404 /40x.jpg;
	#error_page 500 502 503 404 /50x.jpg;
	
	#精準定位錯誤類型
	error_page 403 /403.jpg;
    error_page 404 /404.jpg;
	
	#精準匹配訪問
	location = /404.jpg {
    	root /code/err;		這會到/code/err目錄下找404.jpg
    }
	location = /50x.jpg {
    	root /code/err;
    }	
}

8.location優先級

一個server出現多個location

完整匹配 優先級高
= 進行普通字符精確匹配, 徹底匹配
^~ 表示普通字符匹配, 使用前綴匹配
正則匹配 匹配後會繼續查找更精確匹配的location
~ 區分大小寫匹配
~* 不區分大小寫

8.1.實例準備

[root@Nginx conf.d]# cat testserver.conf 
server {
        listen 80;
        server_name 192.168.69.113;
        root /soft;
        index index.html;

        location = /code1/ {
               rewrite ^(.*)$ /code1/index.html break;
        }

        location ~ /code* {
        rewrite ^(.*)$ /code3/index.html break;
        }
        location ^~ /code {
                rewrite ^(.*)$ /code2/index.html break;
        }
}

8.2.測試效果

[root@Nginx conf.d]# curl http://192.168.69.113/code1/
<h1>Code 1</h1>

//註釋掉精確匹配=, 重啓Nginx
[root@Nginx ~]# curl http://192.168.69.113/code1/
<h1>Code 2</h1>

//註釋掉^~, 重啓Nginx
[root@Nginx ~]# curl http://192.168.69.113/code1/
<h1>Code 3</h1>

9.獲取用戶真實IP

Nginx傳遞用戶的真實IP地址 $remote_addr 只能獲取到最近一臺服務器訪問IP

  • x_forwarded_for 頭部信息容易被篡改
  • nginx Realip_module

9.1第一種方法:x_forwarded_for

9.1.一、準備三臺服務器

角色 外網IP(NAT) 內網IP(LAN) 安裝工具
web01 eth0:10.0.0.7 eth1:172.16.1.7 nginx
lb01 eth0:10.0.0.5 eth1:172.16.1.5 nginx
lb02 eth0:10.0.0.6 eth1:172.16.1.6 nginx

9.1.二、使用官方倉庫安裝Nginx

[root@web01 ~]# cat /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
 
#安裝Nginx
[root@web01 ~]# yum install nginx -y

9.1.三、啓動三臺nginx

systemctl start nginx
systemctl enable nginx

9.1.四、web01上配置

修改日誌格式

寫配置文件

[root@web01 conf.d]# cat proxy.web.xly.com.conf 
server {
	listen 80;
	server_name web.xly.com;
	
	location / {
		root /web;
		index index.html;
	}
}

9.1.五、lb01上的配置

[root@lb01 conf.d]# vim proxy.web.xly.com.conf 
server {
        listen 80;
        server_name web.xly.com;

        location / {
                proxy_pass http://10.0.0.6:80;
                include proxy_params;
        }
}

9.1.六、lb02上的配置

[root@lb02 conf.d]# cat proxy.web.xly.com.conf 
server {
	listen 80;
	server_name web.xly.com;

	location / {
		proxy_pass http://10.0.0.7:80;
		include proxy_params;
	}
}

9.1.七、本地hosts解析

10.0.0.5 web.xly.com

9.1.八、瀏覽器訪問

9.1.九、查看日誌

9.2第二種方法:nginx Realip_module

使用nginx Realip_module獲取多級代理下的客戶端真實IP地址,須要在Web上配置

set_real_ip_from  10.0.0.5;
set_real_ip_from  10.0.0.6;
set_real_ip_from  10.0.0.7;
real_ip_header    X-Forwarded-For;
real_ip_recursive on;

set_real_ip_from:真實服務器上一級代理的IP地址或者IP段,能夠寫多行 real_ip_header:從哪一個header頭檢索出須要的IP地址 real_ip_recursive:遞歸排除set_real_ip_from裏面出現的IP,其他沒有出現的認爲是用戶真實IP 例如: "10.0.0.1, 10.0.0.5, 10.0.0.6" 10.0.0.5,10.0.0.6都出如今set_real_ip_from中,僅僅10.0.0.1沒出現,那麼他就被認爲是用戶的ip地址,而且賦值到$remote_addr變量

10.常見HTTP狀態碼

200 正常請求
301 永久跳轉
302 臨時跳轉
400 請求參數錯誤
401 帳戶密碼錯誤(authorization required)
403 權限被拒絕(forbidden)
404 文件沒找到(Not Found)
413 用戶上傳文件大小限制(Request Entity Too Large)
502 後端服務無響應(boy gateway)
504 後端服務執行超時(Gateway Time-out)

11.網站訪問原理

網站相關術語 若是一棟大廈裏全部工做人員經過1個IP公網接口上網, 總共100個設備, 當全部人同時請求一個網站, 而且刷新了5次, 那麼請求pv、ip、uv分別是多少

pv:頁面瀏覽量 500 uv:惟一設備100 ip:惟一出口 1

11.1.DNS流程

  • 1.查詢本地Hosts
  • 2.請求本地localDNS
  • 3.返回對應的IP

11.2.HTTP鏈接

  • 1.創建TCP三次握手,發送請求內容, 請求頭、請求的行、請求的主體
  • 2.將請求傳遞給負載均衡, 負載均衡作對應的調度
  • 3.若是請求的是靜態頁面, 那麼調度至對應的靜態集羣組便可
  • 4.若是請求的是動態頁面, 將請求調度至動態集羣組
    • 1.若是僅僅是請求頁面, 可能會通過Opcache緩存返回
    • 2.若是請求頁面須要查詢數據庫, 或者是往數據庫插入內容
    • 3.檢查對應的操做是查詢仍是寫入, 若是是查詢數據庫
    • 4.檢查查詢的內容是否有被緩存, 若有緩存則返回
    • 5.檢查查詢語句, 將查詢結果返回
    • 6.內存緩存Redis緩存對應的查詢結果
    • 7.返回對應客戶端請求的內容至於WEB節點
    • 8.WEB節點收到請求後返回內容至負載均衡
    • 9.負載均衡返回客戶端內容, TCP四次斷開

11.3.HTTP斷開鏈接

面試時需注意: 1.按照分層結構 CDN層->負載層->WEB層->存儲層->緩存層->數據庫層 同時須要注意, 每一層都有對應的緩存機制

12.Nginx優化方案

Nginx優化

1.gzip壓縮
2.expires靜態文件緩存
3.調整網絡IO模型,調整Nginx worker進程的最大鏈接數
5.隱藏Nginx名稱和版本號
6.配置防盜鏈,防止資源被盜用
7.禁止經過IP地址訪問,禁止惡意域名解析,只容許域名訪問
8.防DDOS、cc攻擊, 限制單IP併發請求鏈接
9.配置錯誤頁面,根據錯誤代碼指定網頁反饋用戶
10.限制上傳資源目錄被程序訪問,防止木馬入侵系統
11.Nginx加密傳輸優化

13.Nginx架構總結

基於Nginx中間件的架構

13.1.瞭解需求(定義Nginx在服務體系中的角色)

靜態資源服務的功能設計
    類型分類(視頻、圖片、html)
    瀏覽器緩存
    防盜鏈
    流量限制
    防資源盜用
    壓縮(壓縮模式, 壓縮比例, 壓縮類型)
    代理服務
    協議類型
    正向代理
    反向代理
    負載均衡
    代理緩存
    頭信息處理
    Proxy_Pass
    LNMP
    動靜分離

13.2.設計評估

硬件 CPU、內存、磁盤
    系統(用戶權限、日誌目錄存放)
    代理服務/負載均衡 (CPU、內存)
    靜態資源服務(硬盤容量、硬盤轉速)
    動態資源服務(硬盤轉速、讀寫效率)
    緩存資源服務(SSD固態)

13.3.配置注意事項

合理配置
    瞭解原理
    http協議原理
    http狀態原理
    操做系統原理
    關注日誌
    日誌是否有打開
    是否有對應請求
    請求狀態碼信息符合
    錯誤日誌信息吐出來
    錯誤日誌內容和含義
相關文章
相關標籤/搜索