Nginx使用總結

localhost02發表於2018-12-22

Nginx安裝

安裝

tar zxvf nginx-1.2.9.tar.gz   #解壓nginx
cd nginx-1.2.9   #進入目錄
./configure --prefix=/opt/soft/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module   #配置安裝模組
make install   #安裝
複製程式碼

--prefix:指定安裝目錄,預設的安裝目錄是/usr/local/nginx;

--with-http_ssl_module:安裝https服務模組

啟動

/opt/soft/nginx/sbin/nginx
/opt/soft/nginx/sbin/nginx -s stop   # fast shutdown
/opt/soft/nginx/sbin/nginx -s quit   # graceful shutdown
/opt/soft/nginx/sbin/nginx -s reload   # reloading the configuration file
/opt/soft/nginx/sbin/nginx -s reopen   # reopening the log files
複製程式碼

設定開機自啟動:

echo "/opt/soft/nginx/sbin/nginx -c /opt/soft/nginx/conf/nginx.conf" >> /etc/rc.local
複製程式碼

Nignx配置虛擬主機、反向代理、負載均衡

虛擬主機

主要配置server模組的 listen 和 server_name

基於域名

server {
	listen 80;
	server_name test.a.com;
	location / {
		proxy_pass http://192.168.0.1;   #反向代理到其他站點
	}
}
server {
	listen 80;
	server_name test.b.com;
	location / {
		proxy_pass http://192.168.0.2;   #反向代理到其他站點
	}
}
複製程式碼

注意: 配置檔案下載伺服器

server {
	listen       80;
	server_name  file.download.com;
	charset utf-8;
	location ~ ^/(.*)$ {
		add_header Content-Disposition "attachment; filename=$1";   #設定header
		alias "C:/Robot_Download/$1";   #檔案的本地位置
	}
}

複製程式碼

基於埠

server {
	listen 80;
	server_name localhost;
	alias /data/html/index.html;   #也可使用root、location等方式指向靜態資源
}
server {
	listen 81;
	server_name localhost;
	root /data/html/index.html;   #也可使用alias、location等方式指向靜態資源
}
複製程式碼

基於ip

server {
	listen 100.100.100.100:80;
	server_name localhost;
	location / {
		alias /data/html/index.html;   #也可使用alias、root等方式指向靜態資源
	}
}
server {
	listen 100.100.100.101:80;
	server_name localhost;
	location / {
		alias /data/html/index.html;   #也可使用alias、root等方式指向靜態資源
	}
}
複製程式碼

反向代理

主要配置location模組的 proxy_pass

server {
	listen 80;
	server_name test.b.com;
	location / {
		proxy_pass http://192.168.0.2;   #反向代理到其他應用伺服器或web伺服器
	}
}
複製程式碼

負載均衡

主要配置upstream和location模組的proxy_pass

upstream tomcat_server_pool{
	ip_hash;
	server 127.0.0.1:8090 weight=10;   #設定訪問權重,權重越高越容易被訪問
	server 127.0.0.1:8100 weight=10;
	server 127.0.0.1:8110 weight=7;
}
server {
	listen 80;
	server_name test.b.com;
	location / {
		proxy_pass http://tomcat_server_pool;   #反向代理到其他伺服器集合
	}
}
複製程式碼

**ip_hash:**使用ip_hash策略的負載均衡解決session問題。每個請求按訪問ip的hash結果分配,這樣每個訪客固定訪問一個後端伺服器,可較好地解決session的問題。

location對映規則

alias和root的區別

location  /svn/ {
	root /data/ftp/;
	autoindex on;
}
複製程式碼

訪問127.0.0.1/svn/a.jpg:則會進入到 /data/ftp/svn/a.jpg

location  /svn/ {
	alias /data/ftp/;
	autoindex on;
}
複製程式碼

訪問127.0.0.1/svn/a.jpg:則會進入到 /data/ftp/a.jpg

proxy_pass的url是否存在 / 的區別

注意:alias和root後的url都是要加/的

location  /proxy/ {
	proxy_pass http://127.0.0.1/;
}
複製程式碼

訪問:127.0.0.1/proxy/a.jpg:則會請求到:http://127.0.0.1/a.jpg

location  /proxy/ {
	proxy_pass http://127.0.0.1;
}
複製程式碼

訪問:127.0.0.1/proxy/a.jpg:則會請求到:http://127.0.0.1/proxy/a.jpg

“location /xxx/” 與“location ^~ /xxx/”區別

location = / {   #表示匹配訪問根目錄
	root   html;   #當前安裝目錄下的html,/html則表示伺服器根目錄下的html
	index  index.html index.htm;
}
location /svn/ {   #表示匹配ip:port/svn/
	root /data/;
	autoindex on;
}
}
location ^~ /svn/ {   #表示只要含有svn/就會被匹配
	root /data/;
	autoindex on;
}
複製程式碼

“location /xxx/”表示匹配ip:port/xxx,需注意:

  1. 能匹配到 test.com/xxx/home.jpg;
  2. 不能匹配到 test.com/folder/xxx/home.jpg;
  3. 如果需要匹配到後者,應改為:location /folder/xxx/

科普:

一般預設都有location = /(精確匹配),而還有一種是 location /(模糊匹配)

兩者的區別是:模糊匹配就算匹配到也會一直匹配下去,而精確匹配不會。

例: 如上例中,把location = /換成location /,那麼請求 112.74.55.239/svn/

  1. 先會匹配 /,請求的物理路徑變成了:/usr/local/nginx/html
  2. 繼續匹配/svn/,實際訪問的物理路徑變成了: /usr/local/nginx/html/data/svn/

rewrite

location ~ \.php${
	rewirte "^/php/(.*)$" http://localhost:8090/$1
}
複製程式碼
  • localhost/php/test.php重定向到localhost:8090/test.php。如果正規表示式(regex)匹配到了請求的URI(request URI),這個URI會被後面的replacement替換
  • 如果正規表示式(regex)裡包含“}” or “;”字元,需要用單引號或者雙引號把正規表示式引起來
  • 如果replacement字串裡有新的request引數,那麼之前的引數會附加到其後面,如果要避免這種情況,那就在replacement字串後面加上“?”,eg: rewrite ^/users/(.*)$ /show?user=$1? last;=

可選的flag引數如下:

  1. last
  • 結束當前的請求處理,用替換後的URI重新匹配location;
  • 可理解為重寫(rewrite)後,發起了一個新請求,進入server模組,匹配location;
  • 如果重新匹配迴圈的次數超過10次,nginx會返回500錯誤;
  • 返回302 http狀態碼 ;
  • 瀏覽器位址列顯示重定向後的url
  1. break
  • 結束當前的請求處理,使用當前資源,不在執行location裡餘下的語句;
  • 返回302 http狀態碼 ;
  • 瀏覽器位址列顯示重定向後的url
  1. redirect
  • 臨時跳轉,返回302 http狀態碼;
  • 瀏覽器位址列顯示重定向後的url
  1. permanent
  • 永久跳轉,返回301 http狀態碼;
  • 瀏覽器位址列顯示重定向後的url

常用配置

支援後端獲取真實客戶端IP,而不是該代理的IP

proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
複製程式碼

解決request.getScheme()獲取不到真實協議

proxy_set_header X-Forwarded-Proto $scheme;
複製程式碼

websocket配置

map $http_upgrade $connection_upgrade {
default upgrade;
	'' close;
}
複製程式碼