1. nginx常見配置
http服務上支持【若干虛擬主機】。每個虛擬主機對應一個server配置項,配置項里面包含該虛擬主機相關的配置。
server{listen 80 default;server_name www.yonqin.com;index index.html index.htm index.php;root /data/www;location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)${expires 30d;}location ~ .*\.(js|css)?${expires 1h;}
}
listen 80;
?監聽端口,默認80,小于1024的要以root啟動。可以為listen :80、listen 127.0.0.1:80等形式。server_name www.yonqin.com
?用于設置虛擬主機服務名稱,如:127.0.0.1 、 localhost 、域名[www.baidu.comopen in new window?|?www.jd.comopen in new window],也可以進行正則匹配。
root /data/www
?定義服務器的默認網站根目錄位置。可以是linux的絕對路徑(/xxx/xx),也可以是nginx安裝目錄的相對路徑(html)。index index.jsp index.html index.htm
?:定義路徑下默認訪問的文件名,一般跟著root放。
1.1 location 常見的配置項:
location通常用來匹配uri,其基本語法如下:
location [=|~|~*|^~] /uri/ {}
(1)=表示匹配uri時必須做到完全匹配,如
location = /index {}
(2)~表示匹配URI時是字母大小寫敏感的,可以使用正則表達式。
(3)~*表示匹配URI時是忽略字母大小敏感的,可以使用正則表達式。
(4)^~表示匹配uri時只需滿足前綴匹配即可
# 所有 /./img/開頭的uri會全部匹配
location ^~ /./img/ {}
(5)uri參數中是可以使用正則表達式的,如匹配以 .gif .jpg和.jpeg結尾的uri,如下:
location ~* \.(gif|jpg|jpeg)$ {}
(6)以下方式可以匹配所有的uri
location / {}
(7)@
?指定一個命名的location,一般用于內部重定義請求:
location @name {…}
1.2文件路徑的定義:
(1)以root方式設置資源路徑
語法 root path ,默認 root html,可以在http、server、location模塊中配置。
location ^~ /backend {root /data/www/backend
}
如果url為?/backend/index/test.html
則會返回/data/www/backend/backend/index/test.html這個文件。
(2)以alias方式設置資源路徑
alias也是用來設置文件資源的,它和root不同點在于如何解讀緊跟location后面的uri參數,可以在location中配置:
location ^~ /backend {alias /data/www/backend
}
如果url為?/backend/index/test.html
則會返回/data/www/backend/index/test.html文件。
alias會將location后的url部分丟棄掉,而root不會。
1.3 nginx做靜態服務器
我們都知道,nginx的安裝目錄中有這樣一個html的文件夾,這里只做一個基本的演示,進入后進行簡單的修改即可, 修改index.html文件看 可以生效。
我們再結合nginx的基礎配置文件中的以下內容:
server {listen 80;server_name localhost;location / {root html;index index.html index.htm;}
}
進入瀏覽器即可看到效果
2.反向代理解決跨域
在實現代理的過程中,我們需要將/api這個前綴刪除掉,有以下兩種方法,一種是重寫url,如下:
location ^~ /api/ {rewrite ^/api(.*)$ $1 break;proxy_pass http://127.0.0.1:8080;
}
更簡單的做法是,在代理地址的后邊加/
,這樣做也會去掉前綴,但不如以上方式靈活:
location ^~ /api/ {proxy_pass http://127.0.0.1:8080/;
}
3. nginx為后端工程做負載均衡
3.1 upstream模塊解讀
nginx 的負載均衡功能依賴于 ngx_http_upstream_module模塊。upstream 模塊應該放于http{}標簽內。
模塊寫法如下:?
upstream backend {ip_hash; server backend1.example.com;server backend2.example.com:8080;server 127.0.0.1:8080;server backup2.example.com:8080;
}
然后在location處使用如下寫法:
location / {proxy_pass http://backend;
}
以上寫法的意思就是,將來同一個url訪問我們的服務時,服務可以由backend中的服務器按照某種特定規則輪流提供
3.2 ngixn負載均衡的五種算法
(1)round robin 輪詢 (默認) 按時間順序依次將請求分配到各個后臺服務器中,掛掉的服務器自動從列表中剔除
upstream bakend { server 192.168.0.1 down; server 192.168.0.2;
}
(2)weight 輪詢權重 weight的值越大分配到的訪問概率越高,主要用于后端每臺服務器性能不均衡的情況下,或在主從的情況下設置不同的權值,達到合理有效的地利用主機資源。
upstream bakend { server 192.168.0.1 weight=20; server 192.168.0.2 weight=10;
}
(3)ip_hash:每個請求按訪問IP的哈希結果分配,使來自同一個IP的訪客固定訪問一臺后端服務器,并且可以有效解決動態網頁存在的session共享問題。
upstream bakend { ip_hash; server 192.168.0.1:88; server 192.168.0.2:80;
}
(4)url_hash:按訪問的URL的哈希結果來分配請求,使每個URL定向到同一臺后端服務器,可以進一步提高后端服務器緩存的效率 ,Nginx本身不支持url_hash,需要安裝Nginx的hash軟件包。
upstream backend { server 192.168.0.1:88; //使用hash語句時,不能在使用weight等其他參數server 192.168.0.2:80; hash $request_uri; hash_method crc32; //使用hash算法
}
(5)fair算法:可以根據頁面大小和加載時間長短智能地進行負載均衡,根據后端服務器的響應時間來分配請求,響應時間短的優先分配。Nginx本身不支持fair,要安裝upstream_fair模塊才能使用。
upstream backend { server 192.168.0.1:88; server 192.168.0.2:80; fair;
}
簡單的負載均衡的實踐:
1. 創建簡單的SpringBoot項目 ,寫一個controller層的接口 返回當前Java進程的監聽端口號
package spring.learn.nginx;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/nginx")
public class TestContoller {@Autowiredprivate Environment env;@RequestMapping("/test")public String test(){ // 打印當前端口return "nginx yonqin port: "+ env.getProperty("server.port");}
}
2. jar上傳 并啟動
3. 配置nginx.conf?
4.瀏覽器測試
三次請求訪問不同的實例
4. nginx 做資源下載服務器
4.1 訪問控制 allow/deny
Nginx 的訪問控制模塊默認就會安裝,而且寫法也非常簡單,可以分別有多個allow,deny,允許或禁止某個ip或ip段訪問,依次滿足任何一個規則就停止往下匹配。如:
location /status {stub_status on;access_log off;allow 192.168.10.100;allow 172.29.73.0/24;deny all;
}
4.2、列出目錄 autoindex?
打開nginx.conf文件,在location,server 或 http段中加入如下參數 ,通過配置可以做為資源下載站 下。
location ^~ /file {root /data/www;autoindex on;autoindex_exact_size off;autoindex_localtime on;charset utf-8,gbk;
}
autoindex on;
運行列出目錄內容。另外兩個參數最好也加上去。autoindex_exact_size off;
?默認為on,顯示出文件的確切大小,單位是bytes。改為off后,顯示出文件的大概大小,單位是kB或者MB或者GB。autoindex_localtime on;
?默認為off,顯示的文件時間為GMT時間。改為on后,顯示的文件時間為文件的服務器時間。