nginx配置屬性總結(jié)
全局屬性
worker_processes 1; worker進(jìn)程數(shù)量,通常配置為與cpu核數(shù)相同 默認(rèn)為1
events屬性
worker_connections 1024;服務(wù)器與?戶的?絡(luò)連接,?如worker_connections 1024,標(biāo)識(shí)每個(gè)workderprocess?持的最?連接數(shù)為1024
http屬性
http屬性是配置最頻繁的部分,虛擬主機(jī)的配置,監(jiān)聽端?的配置,請(qǐng)求轉(zhuǎn)發(fā)、反向代理、負(fù)載均衡等,后面配置時(shí)再詳細(xì)講解
nginx用法總結(jié)
反向代理
新增tomcat默認(rèn)服務(wù)并啟動(dòng)。配置如下反向代理,修改http屬性下server和location配置為。多個(gè)配置可以配置多個(gè)location使用9999端口統(tǒng)一代理
server {
listen 9999;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://127.0.0.1:8080;
# root html;
# index index.html index.htm;
}
啟動(dòng)的效果為

負(fù)載均衡配置
首先除了之前原有的配置一個(gè)端口為8081且修改原tomcat默認(rèn)頁面Home元素為8081的tomcat并啟動(dòng)
輪詢負(fù)載均衡配置
負(fù)載均衡的配置需要在http的模塊下統(tǒng)一配置upstream屬性,輪詢負(fù)載均衡的配置
upstream roundServer {
server 127.0.0.1:8080;
server 127.0.0.1:8081;
}
同時(shí)在需要配置的路由地址中配置upstream就能實(shí)現(xiàn)nginx的負(fù)載均衡,例如
location /round {
proxy_pass httP://roundServer/;
}
隨后訪問http://119.45.52.68:9999/round 就發(fā)現(xiàn)8081和8080兩個(gè)tomcat的配置交替出現(xiàn)
權(quán)重負(fù)載均衡
默認(rèn)每個(gè)服務(wù)的權(quán)重是一樣的。因此輪詢即是一種正常的權(quán)重相同的負(fù)載均衡
下面將上述配置改為權(quán)重不同的配置
upstream roundServer {
server 127.0.0.1:8080 weight=3;
server 127.0.0.1:8081 weight=1;
}
重新訪問http://119.45.52.68:9999/round 可以發(fā)現(xiàn)8080默認(rèn)的頁面出現(xiàn)三次8081的頁面才出現(xiàn)一次
ip_hash負(fù)載均衡
每個(gè)請(qǐng)求按照ip的hash結(jié)果分配,每?個(gè)客戶端的請(qǐng)求會(huì)固定分配到同?個(gè)?標(biāo)服務(wù)器處理,可
以解決session問題
upstream roundServer {
in_hash;
server 127.0.0.1:8080;
server 127.0.0.1:8081;
}
```
### Nginx的動(dòng)靜分離
由于Nginx很適合用來處理靜態(tài)資源,所以在實(shí)際使用中經(jīng)常用來直接代理靜態(tài)資源
代理靜態(tài)資源配置
```
location /statichtml/ {
root staticData
}
```
此處代理的即是nginx安裝目錄下staticData/statichtml/下的靜態(tài)文件。同樣的也可以代理盤符下其他目錄的文件。路徑配置合理即可
>歡迎關(guān)注和點(diǎn)贊,以及總結(jié)的分類面試題https://github.com/zhendiao/JavaInterview