Nginx多個相同Server_name優先級php
[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
[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.當用戶第一次訪問, 由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>
[root@web01 conf.d]# cat server4.conf server { listen 80 default_server; server_name _; return 503; }
[root@web01 conf.d]# cat server4.conf server { listen 80 default_server; server_name _; return 302 https://www.xuliangwei.com; }
一臺服務器配置多個網站,若是配置都寫在nginx.conf主配置文件中,會致使nginx.conf主配置文件變得很是龐大並且可讀性很是的差。那麼後期的維護就變得麻煩。 假設如今但願快速的關閉一個站點,該怎麼辦?html
Include包含的做用是爲了簡化主配置文件,便於人類可讀。java
inlcude /etc/nginx/online/*.conf #線上使用的配置 /etc/nginx/offline #保留配置,不啓用(下次使用在移動到online中)nginx
root與alias路徑匹配主要區別在於nginx如何解釋location後面的uri,這會使二者分別以不一樣的方式將請求映射到服務器文件上,alias是一個目錄別名的定義,root則是最上層目錄的定義。 root的處理結果是:root路徑+location路徑 alias的處理結果是:使用alias定義的路徑web
[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
[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'
server { listen 80; server_name image.oldboy.com; location / { root /code; } location ~* ^.*\.(png|jpg|gif)$ { 請求圖片回到alias定義的路徑去找 alias /code/images/; } }
[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; } }
$uri匹配文件名,如用戶請求try.oldboy.com/index.html那麼$uri則會上對應的root指定的站點目錄中查找是否存在該文件 $uri/匹配目錄下的文件,如用戶請求try.oldboy.com/,那麼$uri/則會上/對應root指定的站點目錄中查找文件面試
[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
[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
[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
在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; ... }
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; } }
一個server出現多個location
完整匹配 | 優先級高 |
---|---|
= | 進行普通字符精確匹配, 徹底匹配 |
^~ | 表示普通字符匹配, 使用前綴匹配 |
正則匹配 | 匹配後會繼續查找更精確匹配的location |
~ | 區分大小寫匹配 |
~* | 不區分大小寫 |
[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; } }
[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>
Nginx傳遞用戶的真實IP地址 $remote_addr 只能獲取到最近一臺服務器訪問IP
角色 | 外網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 |
[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
systemctl start nginx systemctl enable nginx
修改日誌格式
寫配置文件
[root@web01 conf.d]# cat proxy.web.xly.com.conf server { listen 80; server_name web.xly.com; location / { root /web; index index.html; } }
[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; } }
[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; } }
10.0.0.5 web.xly.com
使用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變量
200 正常請求 301 永久跳轉 302 臨時跳轉 400 請求參數錯誤 401 帳戶密碼錯誤(authorization required) 403 權限被拒絕(forbidden) 404 文件沒找到(Not Found) 413 用戶上傳文件大小限制(Request Entity Too Large) 502 後端服務無響應(boy gateway) 504 後端服務執行超時(Gateway Time-out)
網站相關術語 若是一棟大廈裏全部工做人員經過1個IP公網接口上網, 總共100個設備, 當全部人同時請求一個網站, 而且刷新了5次, 那麼請求pv、ip、uv分別是多少
pv:頁面瀏覽量 500 uv:惟一設備100 ip:惟一出口 1
面試時需注意: 1.按照分層結構 CDN層->負載層->WEB層->存儲層->緩存層->數據庫層 同時須要注意, 每一層都有對應的緩存機制
Nginx優化
1.gzip壓縮 2.expires靜態文件緩存 3.調整網絡IO模型,調整Nginx worker進程的最大鏈接數 5.隱藏Nginx名稱和版本號 6.配置防盜鏈,防止資源被盜用 7.禁止經過IP地址訪問,禁止惡意域名解析,只容許域名訪問 8.防DDOS、cc攻擊, 限制單IP併發請求鏈接 9.配置錯誤頁面,根據錯誤代碼指定網頁反饋用戶 10.限制上傳資源目錄被程序訪問,防止木馬入侵系統 11.Nginx加密傳輸優化
基於Nginx中間件的架構
靜態資源服務的功能設計 類型分類(視頻、圖片、html) 瀏覽器緩存 防盜鏈 流量限制 防資源盜用 壓縮(壓縮模式, 壓縮比例, 壓縮類型) 代理服務 協議類型 正向代理 反向代理 負載均衡 代理緩存 頭信息處理 Proxy_Pass LNMP 動靜分離
硬件 CPU、內存、磁盤 系統(用戶權限、日誌目錄存放) 代理服務/負載均衡 (CPU、內存) 靜態資源服務(硬盤容量、硬盤轉速) 動態資源服務(硬盤轉速、讀寫效率) 緩存資源服務(SSD固態)
合理配置 瞭解原理 http協議原理 http狀態原理 操做系統原理 關注日誌 日誌是否有打開 是否有對應請求 請求狀態碼信息符合 錯誤日誌信息吐出來 錯誤日誌內容和含義