1.nginx下載
下載地址:nginx: download
2.nginx結構分析
conf-------配置文件
contrib-----應用模塊
docs--------說明文檔
html--------首頁/錯誤頁面
logs---------日志文件
temp--------臨時文件
nginx.exe-----主程序
3.nginx配置文件解析
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
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 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
3.1 全局塊(建議修改)
從配置文件開始到 events 塊之間的內(nèi)容,主要會設置一些影響nginx 服務器整體運行的配置指令,主要包括配置運行 Nginx 服務器的用戶(組)、允許生成的 worker process 數(shù),進程 PID 存放路徑、日志存放路徑和類型以及配置文件的引入等。
worker_processes 1;
這是 Nginx 服務器并發(fā)處理服務的關鍵配置,worker_processes 值越大,可以支持的并發(fā)處理量也越多,但是會受到硬件、軟件等設備的制約。
一般客戶環(huán)境建議是cpu的核數(shù)。
3.2 events塊(不建議修改)
events {
worker_connections 1024;
}
events 塊涉及的指令主要影響 Nginx 服務器與用戶的網(wǎng)絡連接,常用的設置包括是否開啟對多 work process 下的網(wǎng)絡連接進行序列化,是否允許同時接收多個網(wǎng)絡連接,選取哪種事件驅(qū)動模型來處理連接請求,每個 word process 可以同時支持的最大連接數(shù)等。
上述例子就表示每個 work process 支持的最大連接數(shù)為 1024.
這部分的配置對 Nginx 的性能影響較大,在實際中應該靈活配置。
3.3 http塊
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
這算是 Nginx 服務器配置中最頻繁的部分,代理、緩存和日志定義等絕大多數(shù)功能和第三方模塊的配置都在這里。
需要注意的是:http 塊也可以包括 http全局塊、server 塊。
①、http 全局塊
http全局塊配置的指令包括文件引入、MIME-TYPE 定義、日志自定義、連接超時時間、單鏈接請求數(shù)上限等。
②、server 塊
這塊和虛擬主機有密切關系,虛擬主機從用戶角度看,和一臺獨立的硬件主機是完全一樣的,該技術的產(chǎn)生是為了節(jié)省互聯(lián)網(wǎng)服務器硬件成本。后面會詳細介紹虛擬主機的概念。
每個 http 塊可以包括多個 server 塊,而每個 server 塊就相當于一個虛擬主機。
而每個 server 塊也分為全局 server 塊,以及可以同時包含多個 locaton 塊。
1、全局 server 塊
最常見的配置是本虛擬機主機的監(jiān)聽配置和本虛擬主機的名稱或IP配置。
2、location 塊
一個 server 塊可以配置多個 location 塊。
這塊的主要作用是基于 Nginx 服務器接收到的請求字符串(例如 server_name/uri-string),對虛擬主機名稱(也可以是IP別名)之外的字符串(例如 前面的 /uri-string)進行匹配,對特定的請求進行處理。地址定向、數(shù)據(jù)緩存和應答控制等功能,還有許多第三方模塊的配置也在這里進行。
4. nginx正向代理
正向代理和反向代理的區(qū)別,一句話就是:如果我們客戶端自己用,就是正向代理。如果是在服務器用,用戶無感知,就是反向代理。
如果把局域網(wǎng)外的 Internet 想象成一個巨大的資源庫,則局域網(wǎng)中的客戶端要訪問 Internet,則需要通過代理服務器來訪問,這種代理服務就稱為正向代理,下面是正向代理的原理圖。
由于工作環(huán)境原因,日常工作只能局限于單位的局域網(wǎng),如果想要訪問互聯(lián)網(wǎng),怎么辦呢?這就需要用到正向代理,用正向代理來進行上網(wǎng)。
5.nginx反向代理
看下面原理圖,就一目了然。其實客戶端對代理是無感知的,因為客戶端不需要任何配置就可以訪問,我們只需要將請求發(fā)送到反向代理服務器,由反向代理服務器去選擇目標服務器獲取數(shù)據(jù)后,在返回給客戶端,此時反向代理服務器和目標服務器對外就是一個服務器,暴露的是代理服務器地址,隱藏了真實服務器 IP 地址。
5.1 反向代理實列1(單個)
實現(xiàn)效果:使用 Nginx 反向代理,訪問 192.168.17.129:80 直接跳轉(zhuǎn)到 127.0.0.1:8080
注意:此處如果要想從192.168.17.129跳轉(zhuǎn)到本機指定的ip,需要修改本機的hosts文件。
配置代碼:
如上配置,Nginx監(jiān)聽 80 端口,訪問域名為 www.123.com(不加端口號時默認為 80 端口),故訪問該域名時會跳轉(zhuǎn)到 127.0.0.1:8080 路徑上。
此處的意思為:nginx 反向代理服務監(jiān)聽 192.168.17.129的80端口,如果有請求過來,則轉(zhuǎn)到proxy_pass配置的對應服務器上,僅此而已。
在location下,同時配置root和proxy_pass選項時,兩個選項只會二選一執(zhí)行
5.2 反向代理實列2(路徑區(qū)分)
實現(xiàn)效果:使用 Nginx 反向代理,根據(jù)訪問的路徑跳轉(zhuǎn)到不同端口的服務中,Nginx 監(jiān)聽端口為 9001
訪問 http://127.0.0.1:9001/edu/ 直接跳轉(zhuǎn)到 127.0.0.1:8081
訪問 http://127.0.0.1:9001/vod/ 直接跳轉(zhuǎn)到 127.0.0.1:8082
第一步,需要準備兩個 tomcat,一個 8001 端口,一個 8002 端口,并準備好測試的頁面
第二步,修改 nginx 的配置文件,在 http 塊中配置 server
.
5.3 反向代理實列3(路徑區(qū)分,涉及靜態(tài)文件配置)
5.4 nginx反向代理實列4(負載均衡)
1.輪詢(默認)
上面配置的就是輪詢,如果其中一個宕機,另外一個不受任何影響
2.weight 權重
weight 代表權重,默認為1,權重越高被分配的客戶端越多
指定輪詢幾率,weight和訪問比率成正比,用于后端服務器性能不均的情況。
3.ip_hash
每個請求按訪問ip的hash值分配,這樣每個訪問客戶端會固定訪問一個后端服務器,可以解決會話Session丟失的問題
4.最少連接
web請求會被轉(zhuǎn)發(fā)到連接數(shù)最少的服務器上
6 nginx https配置
需要SSL證書,域名商提供,或者可以使用阿里的免費一年的證書
不同的服務商 https的有些不同
7.nginx啟動/刷新配置/重啟
需要修改其中的cd目錄
8.另外常見的配置(后續(xù)更新)
設置上傳附件大小