Nginx 安裝
這裡持續更新修正
Nginx 是一款面向性能設計的 HTTP 伺服器,能反向代理 HTTP,HTTPS 和郵件相關(SMTP,POP3,IMAP)的協定連結。并且提供了負載均衡以及 HTTP 緩存。它的設計充分使用異步事件模型,削減上下文排程的開銷,提高伺服器并發能力。采用了子產品化設計,提供了豐富子產品的第三方子產品。
是以關于 Nginx,有這些标簽:「異步」「事件」「子產品化」「高性能」「高并發」「反向代理」「負載均衡」
Linux系統:
Centos 7 x64
Nginx版本:
1.11.5
安裝
安裝依賴
prce(重定向支援)和openssl(https支援,如果不需要https可以不安裝。)
yum -y install pcre*
yum -y install openssl*
CentOS 6.5 我安裝的時候是選擇的“基本伺服器”,預設這兩個包都沒安裝全,是以這兩個都運作安裝即可。
下載下傳
wget http://nginx.org/download/nginx-1.11.5.tar.gz
# 如果沒有安裝wget
# 下載下傳已編譯版本
$ yum install wget
編譯安裝
然後進入目錄編譯安裝,configure參數說明
cd nginx-1.11.5
./configure
安裝報錯誤的話比如:“C compiler cc is not found”,這個就是缺少編譯環境,安裝一下就可以了 yum -y install gcc make gcc-c++ openssl-devel wget
如果沒有error資訊,就可以執行下邊的安裝了:
make
make install
nginx測試
運作下面指令會出現兩個結果
./nginx -t
# nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
# nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
開機自啟動
編輯 vi /lib/systemd/system/nginx.service 檔案,沒有建立一個 touch nginx.service 然後将如下内容根據具體情況進行修改後,添加到nginx.service檔案中:
[Unit]
Description=nginx1.11.5
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
設定開機啟動,使配置生效:
systemctl enable nginx.service
運維
服務管理
# 啟動
/usr/local/nginx/sbin/nginx
# 重新開機
/usr/local/nginx/sbin/nginx -s reload
# 關閉程序
/usr/local/nginx/sbin/nginx -s stop
# 平滑關閉nginx
/usr/local/nginx/sbin/nginx -s quit
# 檢視nginx的安裝狀态,
/usr/local/nginx/sbin/nginx -V
關閉防火牆,或者添加防火牆規則就可以測試了
或者編輯配置檔案:
添加這樣一條開放80端口的規則後儲存:
-A INPUT -m state --state NEW -m tcp -p tcp --dport -j ACCEPT
重新開機服務即可:
重新開機服務防火牆報錯解決
service iptables restart
# Redirecting to /bin/systemctl restart iptables.service
# Failed to restart iptables.service: Unit iptables.service failed to load: No such file or directory.
在CentOS 7或RHEL 7或Fedora中防火牆由 firewalld 來管理,當然你可以還原傳統的管理方式。或則使用新的指令進行管理。
假如采用傳統請執行一下指令:
# 傳統指令
systemctl stop firewalld
systemctl mask firewalld
# 安裝指令
yum install iptables-services
systemctl enable iptables
service iptables restart
nginx解除安裝
如果通過yum安裝,使用下面指令安裝。
yum remove nginx
編譯安裝,删除/usr/local/nginx目錄即可
如果配置了自啟動腳本,也需要删除。
參數說明
參數 | 說明 |
---|---|
--prefix= | Nginx安裝路徑。如果沒有指定,預設為 /usr/local/nginx。 |
--sbin-path= | Nginx可執行檔案安裝路徑。隻能安裝時指定,如果沒有指定,預設為 /sbin/nginx。 |
--conf-path= | 在沒有給定-c選項下預設的nginx.conf的路徑。如果沒有指定,預設為 /conf/nginx.conf。 |
--pid-path= | 在nginx.conf中沒有指定pid指令的情況下,預設的nginx.pid的路徑。如果沒有指定,預設為 /logs/nginx.pid。 |
--lock-path= | nginx.lock檔案的路徑。 |
--error-log-path= | 在nginx.conf中沒有指定error_log指令的情況下,預設的錯誤日志的路徑。如果沒有指定,預設為 /- logs/error.log。 |
--http-log-path= | 在nginx.conf中沒有指定access_log指令的情況下,預設的通路日志的路徑。如果沒有指定,預設為 /- logs/access.log。 |
--user= | 在nginx.conf中沒有指定user指令的情況下,預設的nginx使用的使用者。如果沒有指定,預設為 nobody。 |
--group= | 在nginx.conf中沒有指定user指令的情況下,預設的nginx使用的組。如果沒有指定,預設為 nobody。 |
--builddir=DIR | 指定編譯的目錄 |
--with-rtsig_module | 啟用 rtsig 子產品 |
--with-select_module --without-select_module | 允許或不允許開啟SELECT模式,如果 configure 沒有找到更合适的模式,比如:kqueue(sun os),epoll (linux kenel 2.6+), rtsig(- 實時信号)或者/dev/poll(一種類似select的模式,底層實作與SELECT基本相 同,都是采用輪訓方法) SELECT模式将是預設安裝模式 |
--with-poll_module --without-poll_module | Whether or not to enable the poll module. This module is enabled by, default if a more suitable method such as kqueue, epoll, rtsig or /dev/poll is not discovered by configure. |
--with-http_ssl_module | Enable ngx_http_ssl_module. Enables SSL support and the ability to handle HTTPS requests. Requires OpenSSL. On Debian, this is libssl-dev. 開啟HTTP SSL子產品,使NGINX可以支援HTTPS請求。這個子產品需要已經安裝了OPENSSL,在DEBIAN上是libssl |
--with-http_realip_module | 啟用 ngx_http_realip_module |
--with-http_addition_module | 啟用 ngx_http_addition_module |
--with-http_sub_module | 啟用 ngx_http_sub_module |
--with-http_dav_module | 啟用 ngx_http_dav_module |
--with-http_flv_module | 啟用 ngx_http_flv_module |
--with-http_stub_status_module | 啟用 "server status" 頁 |
--without-http_charset_module | 禁用 ngx_http_charset_module |
--without-http_gzip_module | 禁用 ngx_http_gzip_module. 如果啟用,需要 zlib 。 |
--without-http_ssi_module | 禁用 ngx_http_ssi_module |
--without-http_userid_module | 禁用 ngx_http_userid_module |
--without-http_access_module | 禁用 ngx_http_access_module |
--without-http_auth_basic_module | 禁用 ngx_http_auth_basic_module |
--without-http_autoindex_module | 禁用 ngx_http_autoindex_module |
--without-http_geo_module | 禁用 ngx_http_geo_module |
--without-http_map_module | 禁用 ngx_http_map_module |
--without-http_referer_module | 禁用 ngx_http_referer_module |
--without-http_rewrite_module | 禁用 ngx_http_rewrite_module. 如果啟用需要 PCRE 。 |
--without-http_proxy_module | 禁用 ngx_http_proxy_module |
--without-http_fastcgi_module | 禁用 ngx_http_fastcgi_module |
--without-http_memcached_module | 禁用 ngx_http_memcached_module |
--without-http_limit_zone_module | 禁用 ngx_http_limit_zone_module |
--without-http_empty_gif_module | 禁用 ngx_http_empty_gif_module |
--without-http_browser_module | 禁用 ngx_http_browser_module |
--without-http_upstream_ip_hash_module | 禁用 ngx_http_upstream_ip_hash_module |
--with-http_perl_module | 啟用 ngx_http_perl_module |
--with-perl_modules_path=PATH | 指定 perl 子產品的路徑 |
--with-perl=PATH | 指定 perl 執行檔案的路徑 |
--http-log-path=PATH | Set path to the http access log |
--http-client-body-temp-path=PATH | Set path to the http client request body temporary files |
--http-proxy-temp-path=PATH | Set path to the http proxy temporary files |
--http-fastcgi-temp-path=PATH | Set path to the http fastcgi temporary files |
--without-http | 禁用 HTTP server |
--with-mail | 啟用 IMAP4/POP3/SMTP 代理子產品 |
--with-mail_ssl_module | 啟用 ngx_mail_ssl_module |
--with-cc=PATH | 指定 C 編譯器的路徑 |
--with-cpp=PATH | 指定 C 預處理器的路徑 |
--with-cc-opt=OPTIONS | Additional parameters which will be added to the variable CFLAGS. With the use of the system library PCRE in FreeBSD, it is necessary to indicate --with-cc-opt="-I /usr/local/include". If we are using select() and it is necessary to increase the number of file descriptors, then this also can be assigned here: --with-cc-opt="-D FD_SETSIZE=2048". |
--with-ld-opt=OPTIONS | Additional parameters passed to the linker. With the use of the system library PCRE in - FreeBSD, it is necessary to indicate --with-ld-opt="-L /usr/local/lib". |
--with-cpu-opt=CPU | 為特定的 CPU 編譯,有效的值包括:pentium, pentiumpro, pentium3, pentium4, athlon, opteron, amd64, sparc32, sparc64, ppc64 |
--without-pcre | 禁止 PCRE 庫的使用。同時也會禁止 HTTP rewrite 子產品。在 "location" 配置指令中的正規表達式也需要 PCRE 。 |
--with-pcre=DIR | 指定 PCRE 庫的源代碼的路徑。 |
--with-pcre-opt=OPTIONS | Set additional options for PCRE building. |
--with-md5=DIR | Set path to md5 library sources. |
--with-md5-opt=OPTIONS | Set additional options for md5 building. |
--with-md5-asm | Use md5 assembler sources. |
--with-sha1=DIR | Set path to sha1 library sources. |
--with-sha1-opt=OPTIONS | Set additional options for sha1 building. |
--with-sha1-asm | Use sha1 assembler sources. |
--with-zlib=DIR | Set path to zlib library sources. |
--with-zlib-opt=OPTIONS | Set additional options for zlib building. |
--with-zlib-asm=CPU | Use zlib assembler sources optimized for specified CPU, valid values are: pentium, pentiumpro |
--with-openssl=DIR | Set path to OpenSSL library sources |
--with-openssl-opt=OPTIONS | Set additional options for OpenSSL building |
--with-debug | 啟用調試日志 |
--add-module=PATH | Add in a third-party module found in directory PATH |
配置
在Centos 預設配置檔案在 /usr/local/nginx-1.5.1/conf/nginx.conf 我們要在這裡配置一些檔案。nginx.conf是主配置檔案,由若幹個部分組成,每個大括号
{}
表示一個部分。每一行指令都由分号結束
;
,标志着一行的結束。
配置檔案
nginx 的配置系統由一個主配置檔案和其他一些輔助的配置檔案構成。這些配置檔案均是純文字檔案,全部位于 nginx 安裝目錄下的 conf 目錄下。
指令由 nginx 的各個子產品提供,不同的子產品會提供不同的指令來實作配置。
指令除了 Key-Value 的形式,還有作用域指令。
nginx.conf 中的配置資訊,根據其邏輯上的意義,對它們進行了分類,也就是分成了多個作用域,或者稱之為配置指令上下文。不同的作用域含有一個或者多個配置項。
下面的這些上下文指令是用的比較多:
Directive | Description | Contains Directive |
---|---|---|
main | nginx 在運作時與具體業務功能(比如 http 服務或者 email 服務代理)無關的一些參數,比如工作程序數,運作的身份等。 | user, worker_processes, error_log, events, http, mail |
http | 與提供 http 服務相關的一些配置參數。例如:是否使用 keepalive 啊,是否使用 gzip 進行壓縮等。 | server |
server | http 服務上支援若幹虛拟主機。每個虛拟主機一個對應的 server 配置項,配置項裡面包含該虛拟主機相關的配置。在提供 mail 服務的代理時,也可以建立若幹 server. 每個 server 通過監聽的位址來區分。 | listen, server_name, access_log, location, protocol, proxy, smtp_auth, xclient |
location | http 服務中,某些特定的 URL 對應的一系列配置項。 | index, root |
實作 email 相關的 SMTP/IMAP/POP3 代理時,共享的一些配置項(因為可能實作多個代理,工作在多個監聽位址上)。 | server, http, imap_capabilities | |
include | 以便增強配置檔案的可讀性,使得部配置設定置檔案可以重新使用。 | - |
valid_referers | 用來校驗Http請求頭Referer是否有效。 | - |
try_files | 用在server部分,不過最常見的還是用在location部分,它會按照給定的參數順序進行嘗試,第一個被比對到的将會被使用。 | - |
if | 當在location塊中使用if指令,在某些情況下它并不按照預期運作,一般來說避免使用if指令。 | - |
例如我們再 nginx.conf 裡面引用兩個配置 vhost/example.com.conf 和 vhost/gitlab.com.conf 它們都被放在一個我自己建立的目錄 vhost 下面。nginx.conf 配置如下:
worker_processes ;
events {
worker_connections ;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout ;
#gzip on;
server {
listen ;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page /50x.html;
location = /50x.html {
root html;
}
}
include vhost/example.com.conf;
include vhost/gitlab.com.conf;
}
簡單的配置: example.com.conf
server {
#偵聽的80端口
listen ;
server_name baidu.com app.baidu.com; # 這裡指定域名
index index.html index.htm; # 這裡指定預設入口頁面
root /home/www/app.baidu.com; # 這裡指定目錄
}
内置預定義變量
Nginx提供了許多預定義的變量,也可以通過使用set來設定變量。你可以在if中使用預定義變量,也可以将它們傳遞給代理伺服器。以下是一些常見的預定義變量,更多詳見
變量名稱 | 值 |
---|---|
$args_name | 在請求中的name參數 |
$args | 所有請求參數 |
$query_string | $args的别名 |
$content_length | 請求頭Content-Length的值 |
$content_type | 請求頭Content-Type的值 |
$host | 如果目前有Host,則為請求頭Host的值;如果沒有這個頭,那麼該值等于比對該請求的server_name的值 |
$remote_addr | 用戶端的IP位址 |
$request | 完整的請求,從用戶端收到,包括Http請求方法、URI、Http協定、頭、請求體 |
$request_uri | 完整請求的URI,從用戶端來的請求,包括參數 |
$scheme | 目前請求的協定 |
$uri | 目前請求的标準化URI |
反向代理
反向代理是一個Web伺服器,它接受用戶端的連接配接請求,然後将請求轉發給上遊伺服器,并将從伺服器得到的結果傳回給連接配接的用戶端。下面簡單的反向代理的例子:
server {
listen ;
server_name localhost;
client_max_body_size ; # 允許用戶端請求的最大單檔案位元組數
location / {
proxy_pass http://localhost:8080;
proxy_set_header Host $host:$server_port;
}
}
複雜的配置: gitlab.com.conf。
server {
#偵聽的80端口
listen ;
server_name git.example.cn;
location / {
proxy_pass http://localhost:3000;
#以下是一些反向代理的配置可删除
proxy_redirect off;
#後端的Web伺服器可以通過X-Forwarded-For擷取使用者真實IP
proxy_set_header Host $host;
client_max_body_size ; #允許用戶端請求的最大單檔案位元組數
client_body_buffer_size ; #緩沖區代理緩沖使用者端請求的最大位元組數
proxy_connect_timeout ; #nginx跟後端伺服器連接配接逾時時間(代理連接配接逾時)
proxy_send_timeout ; #後端伺服器資料回傳時間(代理發送逾時)
proxy_read_timeout ; #連接配接成功後,後端伺服器響應時間(代理接收逾時)
proxy_buffer_size ; #設定代理伺服器(nginx)儲存使用者頭資訊的緩沖區大小
proxy_buffers ; #proxy_buffers緩沖區,網頁平均在32k以下的話,這樣設定
proxy_busy_buffers_size ; #高負荷下緩沖大小(proxy_buffers*2)
}
}
代理到上遊伺服器的配置中,最重要的是proxy_pass指令。以下是代理子產品中的一些常用指令:
指令 | 說明 |
---|---|
proxy_connect_timeout | Nginx從接受請求至連接配接到上遊伺服器的最長等待時間 |
proxy_send_timeout | 後端伺服器資料回傳時間(代理發送逾時) |
proxy_read_timeout | 連接配接成功後,後端伺服器響應時間(代理接收逾時) |
proxy_cookie_domain | 替代從上遊伺服器來的Set-Cookie頭的domain屬性 |
proxy_cookie_path | 替代從上遊伺服器來的Set-Cookie頭的path屬性 |
proxy_buffer_size | 設定代理伺服器(nginx)儲存使用者頭資訊的緩沖區大小 |
proxy_buffers | proxy_buffers緩沖區,網頁平均在多少k以下 |
proxy_set_header | 重寫發送到上遊伺服器頭的内容,也可以通過将某個頭部的值設定為空字元串,而不發送某個頭部的方法實作 |
proxy_ignore_headers | 這個指令禁止處理來自代理伺服器的應答。 |
proxy_intercept_errors | 使nginx阻止HTTP應答代碼為400或者更高的應答。 |
負載均衡
upstream指令啟用一個新的配置區段,在該區段定義一組上遊伺服器。這些伺服器可能被設定不同的權重,也可能出于對伺服器進行維護,标記為down。
upstream gitlab {
ip_hash;
server ;
server ;
server down;
keepalive ;
}
server {
#偵聽的80端口
listen ;
server_name git.example.cn;
location / {
proxy_pass http://gitlab; #在這裡設定一個代理,和upstream的名字一樣
#以下是一些反向代理的配置可删除
proxy_redirect off;
#後端的Web伺服器可以通過X-Forwarded-For擷取使用者真實IP
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size ; #允許用戶端請求的最大單檔案位元組數
client_body_buffer_size ; #緩沖區代理緩沖使用者端請求的最大位元組數
proxy_connect_timeout ; #nginx跟後端伺服器連接配接逾時時間(代理連接配接逾時)
proxy_send_timeout ; #後端伺服器資料回傳時間(代理發送逾時)
proxy_read_timeout ; #連接配接成功後,後端伺服器響應時間(代理接收逾時)
proxy_buffer_size ; #設定代理伺服器(nginx)儲存使用者頭資訊的緩沖區大小
proxy_buffers ;# 緩沖區,網頁平均在32k以下的話,這樣設定
proxy_busy_buffers_size ; #高負荷下緩沖大小(proxy_buffers*2)
proxy_temp_file_write_size ; #設定緩存檔案夾大小,大于這個值,将從upstream伺服器傳
}
}
每個請求按時間順序逐一配置設定到不同的後端伺服器,如果後端伺服器down掉,能自動剔除。
負載均衡:
upstream子產品能夠使用3種負載均衡算法:輪詢、IP哈希、最少連接配接數。
輪詢: 預設情況下使用輪詢算法,不需要配置指令來激活它,它是基于在隊列中誰是下一個的原理確定通路均勻地分布到每個上遊伺服器;
IP哈希: 通過ip_hash指令來激活,Nginx通過IPv4位址的前3個位元組或者整個IPv6位址作為哈希鍵來實作,同一個IP位址總是能被映射到同一個上遊伺服器;
最少連接配接數: 通過least_conn指令來激活,該算法通過選擇一個活躍數最少的上遊伺服器進行連接配接。如果上遊伺服器處理能力不同,可以通過給server配置weight權重來說明,該算法将考慮到不同伺服器的權重最少連接配接數。
RR
簡單配置 ,這裡我配置了2台伺服器,當然實際上是一台,隻是端口不一樣而已,而8081的伺服器是不存在的,也就是說通路不到,但是我們通路
http://localhost
的時候,也不會有問題,會預設跳轉到
http://localhost:8080
具體是因為Nginx會自動判斷伺服器的狀态,如果伺服器處于不能通路(伺服器挂了),就不會跳轉到這台伺服器,是以也避免了一台伺服器挂了影響使用的情況,由于Nginx預設是RR政策,是以我們不需要其他更多的設定
upstream test {
server localhost:;
server localhost:;
}
server {
listen ;
server_name localhost;
client_max_body_size ;
location / {
proxy_pass http://test;
proxy_set_header Host $host:$server_port;
}
}
負載均衡的核心代碼為
upstream test {
server localhost:;
server localhost:;
}
權重
指定輪詢幾率,weight和通路比率成正比,用于後端伺服器性能不均的情況。 例如
upstream test {
server localhost: weight=;
server localhost: weight=;
}
那麼10次一般隻會有1次會通路到8081,而有9次會通路到8080
ip_hash
上面的2種方式都有一個問題,那就是下一個請求來的時候請求可能分發到另外一個伺服器,當我們的程式不是無狀态的時候(采用了session儲存資料),這時候就有一個很大的很問題了,比如把登入資訊儲存到了session中,那麼跳轉到另外一台伺服器的時候就需要重新登入了,是以很多時候我們需要一個客戶隻通路一個伺服器,那麼就需要用iphash了,iphash的每個請求按通路ip的hash結果配置設定,這樣每個訪客固定通路一個後端伺服器,可以解決session的問題。
upstream test {
ip_hash;
server localhost:;
server localhost:;
}
fair
這是個第三方子產品,按後端伺服器的響應時間來配置設定請求,響應時間短的優先配置設定。
upstream backend {
fair;
server localhost:;
server localhost:;
}
url_hash
這是個第三方子產品,按通路url的hash結果來配置設定請求,使每個url定向到同一個後端伺服器,後端伺服器為緩存時比較有效。 在upstream中加入hash語句,server語句中不能寫入weight等其他的參數,hash_method是使用的hash算法
upstream backend {
hash $request_uri;
hash_method crc32;
server localhost:;
server localhost:;
}
以上5種負載均衡各自适用不同情況下使用,是以可以根據實際情況選擇使用哪種政策模式,不過fair和url_hash需要安裝第三方子產品才能使用
server指令可選參數:
- weight:設定一個伺服器的通路權重,數值越高,收到的請求也越多;
- fail_timeout:在這個指定的時間内伺服器必須提供響應,如果在這個時間内沒有收到響應,那麼伺服器将會被标記為down狀态;
- max_fails:設定在fail_timeout時間之内嘗試對一個伺服器連接配接的最大次數,如果超過這個次數,那麼伺服器将會被标記為down;
- down:标記一個伺服器不再接受任何請求;
- backup:一旦其他伺服器當機,那麼有該标記的機器将會接收請求。
keepalive指令:
Nginx伺服器将會為每一個worker進行保持同上遊伺服器的連接配接。
第三方子產品安裝方法
./configure --prefix=/你的安裝目錄 --add-module=/第三方子產品目錄
常見使用場景
跨域問題
在工作中,有時候會遇到一些接口不支援跨域,這時候可以簡單的添加add_headers來支援cors跨域。配置如下:
server {
listen ;
server_name api.xxx.com;
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET,POST,HEAD';
location / {
proxy_pass http://127.0.0.1:3000;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
}
}
上面更改頭資訊,還有一種,使用 rewrite 指令重定向URI來解決跨域問題。
upstream test {
server ;
server localhost:;
}
server {
listen ;
server_name api.xxx.com;
location /{
root html; #去請求../html檔案夾裡的檔案
index index.html index.htm; #首頁響應位址
}
# 用于攔截請求,比對任何以 /api/開頭的位址,
# 比對符合以後,停止往下搜尋正則。
location ^~/api/{
# 代表重寫攔截進來的請求,并且隻能對域名後邊的除去傳遞的參數外的字元串起作用,
# 例如www.a.com/proxy/api/msg?meth=1&par=2重寫,隻對/proxy/api/msg重寫。
# rewrite後面的參數是一個簡單的正則 ^/api/(.*)$,
# $1代表正則中的第一個(),$2代表第二個()的值,以此類推。
rewrite ^/api/(.*)$ /$1 break;
# 把請求代理到其他主機
# 其中 http://www.b.com/ 寫法和 http://www.b.com寫法的差別如下
# 如果你的請求位址是他 http://server/html/test.jsp
# 配置一: http://www.b.com/ 後面有“/”
# 将反向代理成 http://www.b.com/html/test.jsp 通路
# 配置一: http://www.b.com 後面沒有有“/”
# 将反向代理成 http://www.b.com/test.jsp 通路
proxy_pass http://test;
}
}
ssl配置
超文本傳輸安全協定(縮寫:HTTPS,英語:Hypertext Transfer Protocol Secure)是超文本傳輸協定和SSL/TLS的組合,用以提供加密通訊及對網絡伺服器身份的鑒定。HTTPS連接配接經常被用于網際網路上的交易支付和企業資訊系統中敏感資訊的傳輸。HTTPS不應與在RFC 2660中定義的安全超文本傳輸協定(S-HTTP)相混。
HTTPS 目前已經是所有注重隐私和安全的網站的首選,随着技術的不斷發展,HTTPS 網站已不再是大型網站的專利,所有普通的個人站長和部落格均可以自己動手搭建一個安全的加密的網站。
檢視目前nginx編譯選項
sbin/nginx -V
輸出下面内容
nginx version: nginx/
built by gcc (Red Hat ) (GCC)
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_ssl_module --with-http_spdy_module --with-http_stub_status_module --with-pcre
如果依賴的子產品不存在,可以輸入下面指令重新編譯安裝。
HTTPS server
server {
listen ssl;
server_name localhost;
ssl_certificate /usr/local/nginx/conf/vjjhd.crt;
ssl_certificate_key /usr/local/nginx/conf/vjjhd.key;
# 設定ssl/tls會話緩存的類型和大小。如果設定了這個參數一般是shared,buildin可能會參數記憶體碎片,預設是none,和off差不多,停用緩存。如shared:SSL:10m表示我所有的nginx工作程序共享ssl會話緩存,官網介紹說1M可以存放約4000個sessions。
ssl_session_cache shared:SSL:;
# 用戶端可以重用會話緩存中ssl參數的過期時間,内網系統預設5分鐘太短了,可以設成30m即30分鐘甚至4h。
ssl_session_timeout ;
# 選擇加密套件,不同的浏覽器所支援的套件(和順序)可能會不同。
# 這裡指定的是OpenSSL庫能夠識别的寫法,你可以通過 openssl -v cipher 'RC4:HIGH:!aNULL:!MD5'(後面是你所指定的套件加密算法) 來看所支援算法。
ssl_ciphers HIGH:!aNULL:!MD5;
# 設定協商加密算法時,優先使用我們服務端的加密套件,而不是用戶端浏覽器的加密套件。
ssl_prefer_server_ciphers on;
location / {
root html;
index index.html index.htm;
}
}
出處:https://segmentfault.com/a/1190000008866185