1. 程式人生 > >一文帶你瞭解nginx基礎

一文帶你瞭解nginx基礎

學習nginx,就要先了解什麼是nginx,為什麼使用nginx,最後才是瞭解怎麼使用nginx - [nginx簡介](#profile) - [nginx安裝](#install) - [一、Linux中安裝nginx](#linux) - [二、Docker中安裝nginx](#docker) - [三、檔案目錄](#file) - [nginx常用功能和配置](#function) - [一、限流](#limit) - [二、安全配置](#security) - [三、程序數、併發數、系統優化](#optimize) - [四、GZIP](#gzip) - [五、狀態監控](#status) - [六、負載均衡](#balance) - [七、access日誌切割](#log) - [八、動靜分離](#sepa) - [nginx.conf基礎配置項](#conf)

nginx簡介

  nginx是一個高效能的HTTP和反向代理Web伺服器,還支援正向代理、透明代理、負載均衡、HTTP快取等功能。nginx始於2004年10月4日,使用C語言編寫,2013年4月24日,nginx釋出了v1.4.0穩定版,建議大家使用的版本高於此版本。
  免費開源的nginx能夠在眾多同類產品中脫穎而出,是因為它具備低記憶體、高併發的優勢,且配置簡單,支援URL重寫、GZIP,內建健康檢查,能自動檢測叢集伺服器狀態,跳過宕機伺服器。

nginx安裝

一、Linux中安裝nginx
###### 1.安裝編譯工具和庫檔案 ``` yum -y install make pcre pcre-devel zlib zlib-devel gcc-c++ libtool openssl openssl-devel ``` ###### 2.下載nginx並安裝 - nginx下載地址:[http://nginx.org/en/download.html](http://nginx.org/en/download.html) ``` cd /usr/local/src/ # 下載特定版本 nginx 壓縮包,這裡以v1.18.0為例 wget http://nginx.org/download/nginx-1.18.0.tar.gz # 解壓 tar -zxvf nginx-1.18.0.tar.gz # 進入到 nginx-1.18.0 目錄下 cd nginx-1.18.0/ # 生成 Makefile ./configure # 編譯並且安裝(預設輸出到/usr/local/nginx,可在configure中看到) make && make install ``` - 如果想要編譯輸出到指定目錄 ``` # 設定編譯輸出目錄為 /home/jjj/nginx ./configure --prefix=/home/jjj/nginx # 編譯並且安裝(輸出到/home/jjj/nginx) make && make install ``` ###### 3.防火牆開放埠號 - 開放埠號外網才可訪問 - 練習期間,推薦將防火牆關閉,避免因其導致無法訪問而卡住:`systemctl stop firewalld.service` ``` # 開啟 80 firewall-cmd --zone=public --add-port=80/tcp --permanent # 重啟防火牆 firewall-cmd --reload # 查詢埠號80是否開啟 firewall-cmd --query-port=80/tcp # 查詢哪些埠已開啟 firewall-cmd --list-port ``` ###### 4.自己動手測試一下能否訪問吧 - 啟動nginx(命令要在nginx安裝目錄的sbin目錄下執行) - 在瀏覽器中輸入nginx所在的主機ip ``` /usr/local/nginx/sbin/nginx # 啟動nginx /usr/local/nginx/sbin/nginx -s reload # 重新載入nginx配置 /usr/local/nginx/sbin/nginx -s stop # 快速關閉nginx(先查出nginx進行id,然後使用kill命令強制殺死程序),可能不儲存相關資訊(不推薦) /usr/local/nginx/sbin/nginx -s quit # 平穩關閉nginx,儲存相關資訊(推薦) /usr/local/nginx/sbin/nginx -s reopen # 重新開啟日誌檔案 /usr/local/nginx/sbin/nginx -s # 為 nginx 指定一個配置檔案 /usr/local/nginx/sbin/nginx -t # 不執行,僅測試配置檔案正確性 /usr/local/nginx/sbin/nginx -v # 顯示nginx版本 /usr/local/nginx/sbin/nginx -V # 顯示 nginx 的版本,編譯器版本和配置引數 netstat -anput | grep nginx # 檢測 nginx 執行狀態(如無任何資訊顯示則是未執行) ``` ###### 4.配置開機啟動 關機重啟了你不會想自己動手再啟動一遍nginx吧? - 新建 nginx.service 檔案 ``` vim /usr/lib/systemd/system/nginx.service ``` - 然後在檔案中新增以下配置 - 配置詳情參考:[https://www.cnblogs.com/yyxianren/articles/10677332.html](https://www.cnblogs.com/yyxianren/articles/10677332.html) ``` # 服務說明項 [Unit] # 描述 Description=nginx # 在哪些服務啟動之後啟動該服務,多個服務用空格隔開 After=network.target # 在哪些服務啟動之前啟動該服務 # Before=xxx # 弱依賴,如果xxx服務啟動失敗或停止執行,不影響該服務 # Wants=xxx # 強依賴,如果xxx服務啟動失敗或停止執行,該服務也必須退出 # Requires=xxx # 執行引數設定 [Service] # 啟動型別 # simple(預設值):ExecStart欄位啟動的程序為主程序 # forking:ExecStart欄位將以fork()方式啟動,此時父程序將會退出,子程序將成為主程序 # oneshot:類似於simple,但只執行一次,Systemd 會等它執行完,才啟動其他服務 # dbus:類似於simple,但會等待 D-Bus 訊號後啟動 # notify:類似於simple,啟動結束後會發出通知訊號,然後 Systemd 再啟動其他服務 # idle:類似於simple,但是要等到其他任務都執行完,才會啟動該服務。一種使用場合是為讓該服務的輸出,不與其他服務的輸出相混合 Type=forking # Start命令 ExecStart=/usr/local/nginx/sbin/nginx # Reload命令 ExecReload=/usr/local/nginx/sbin/nginx -s reload # Stop命令 ExecStop=/usr/local/nginx/sbin/nginx -s quit # 是否給服務分配獨立的臨時空間 PrivateTmp=true # 配置開機啟動方式 [Install] # 該服務所在的 Target,Target的含義是服務組,表示一組服務 # multi-user.target 預設被配置為開機啟動 WantedBy=multi-user.target ``` - 純淨版(去除註釋) ``` [Unit] Description=nginx After=network.target [Service] Type=forking ExecStart=/usr/local/nginx/sbin/nginx ExecReload=/usr/local/nginx/sbin/nginx -s reload ExecStop=/usr/local/nginx/sbin/nginx -s quit PrivateTmp=true [Install] WantedBy=multi-user.target ``` - 最後啟動服務並重啟機器進行測試 ``` systemctl enable nginx.service # 啟用 nginx.service 開機啟動 reboot # 重啟電腦 ## 其他命令 systemctl disable nginx.service # 禁止 nginx.service 開機啟動 systemctl stop nginx.service # 停止 nginx.service systemctl start nginx.service  # 啟動 nginx.service systemctl restart nginx.service # 重啟 nginx.service systemctl list-units --type=service # 檢視所有已啟動的服務 ``` - 檢查 nginx.service 執行狀況 ``` systemctl status nginx.service ``` - 檢查nginx執行狀況 ``` netstat -anput | grep nginx ```
二、Docker中安裝nginx
###### 下載映象 ``` # 搜尋映象 docker search nginx # 拉取映象 docker pull nginx ``` ###### 建立並啟動容器 - `-d`:後臺執行 ``` docker run -d -it -p 8080:80 --name nginx8080 nginx ``` ###### 訪問nginx ``` curl :8080 ``` --- ###### 微服務分散式部署前期準備 為了方便修改nginx配置,需要將容器內的nginx相關檔案拷貝到主機中,並將其對映到容器中去,這樣,修改起來就方便多了 - 拷貝nginx檔案到本地 ``` docker cp nginx8080:/etc/nginx ~/nginx ``` - 上面步驟建立的nginx8080已經沒用了,刪除即可 ``` docker stop nginx8080 docker rm nginx8080 ``` ###### 建立掛載本地目錄的nginx8080容器 - 掛載:即主機上指定目錄與容器內指定目錄檔案共享 - `-v ~/nginx:/etc/nginx`:將本地~/nginx檔案對映到容器/etc/nginx,方便修改配置 - `-v ~/nginx/log:/var/log/nginx`:將本地~/nginx/log對映到容器/var/log/nginx,方便檢視日誌 - 我的環境:apidemo1、apidemo1、nginx8080都在同一docker中 - 方法一:使用 link 建立容器間通訊 - `--link=apidemo1:demo1`:實現nginx8080容器到apidemo1容器的單向通訊,併為apidemo1起別名為demo1,nginx8080內部可直接使用demo1訪問apidemo1 - `--link=apidemo2:demo2`:實現nginx8080容器到apidemo2容器的單向通訊,併為apidemo2起別名為demo2,nginx8080內部可直接使用demo2訪問apidemo2 ``` # apidemo1與apidemo2是同一應用的不同分發 docker run -d -it -p 8080:80 --name nginx8080 -v ~/nginx:/etc/nginx -v ~/nginx/log:/var/log/nginx --link=apidemo1:demo1 --link=apidemo2:demo2 nginx ``` - 修改nginx.conf ``` vim ~/nginx/nginx.conf server { listen 80; server_name localhost; location / { root /usr/share/nginx/html; index index.html index.htm; } location /api/ { proxy_pass http://apiserver/api/; } } upstream apiserver { server demo1 weight=1; server demo2 weight=1; } ``` - 方法二:使用 docker bridge 建立容器間通訊 - `docker network ls`:檢視docker網路資訊,其中,我們建立的容器預設使用bridge(橋接) - `docker network inspect bridge`:檢視bridge的詳細資訊以及哪些容器使用了該網路型別,我這邊的ip地址是 - apidemo1:172.17.0.2 - apidemo2:172.17.0.3 ``` docker run -d -it -p 8080:80 --name nginx8080 -v ~/nginx:/etc/nginx -v ~/nginx/log:/var/log/nginx nginx ``` - 修改nginx.conf ``` vim ~/nginx/nginx.conf server { listen 80; server_name localhost; location / { root /usr/share/nginx/html; index index.html index.htm; } location /api/ { proxy_pass http://apiserver/api/; } } upstream apiserver { server 172.17.0.2 weight=1; server 172.17.0.3 weight=1; } ``` ###### 為了應用新nginx.conf,重啟nginx8080 ``` docker restart nginx8080 ``` ###### 測試 ``` // 這裡的路徑按照自己的來 curl :8080/api/values ``` ###### 可能出現的問題 1. Http staus code——502: 解決:檢視日誌可以看到“113: No route to Host”,嘗試關閉docker所在linux機器的防火牆,發現可以訪問了,可以確定是防火牆的問題。但是把防火牆關閉太不安全了,所以我們選擇開啟防火牆,然後執行以下命令 ``` # 信任 docker0 服務連線 nmcli connection modify docker0 connection.zone trusted # 停止 NetworkManager 服務(該服務用語監測網路、自動連線網路) systemctl stop NetworkManager.service # 將docker0網路介面設定為內部區域(永久) firewall-cmd --permanent --zone=trusted --change-interface=docker0 # 啟動 NetworkManager 服務 systemctl start NetworkManager.service # 信任 docker0 服務連線 nmcli connection modify docker0 connection.zone trusted # 重啟 docker systemctl restart docker.service ```
三、檔案目錄
- *_temp:共5個temp結尾的目錄,用於存放nginx執行時產生的臨時檔案 - conf:存放配置檔案的目錄,包含主配置檔案nginx.conf - html:存放了nginx的錯誤頁面和歡迎頁面 - logs:存放了訪問日誌和錯誤日誌 - sbin:存放了nginx的二進位制命令,常用於nginx服務的啟動、停止等管理工作

nginx常用功能和配置

一、限流
###### 三種方式 - limit_conn_zone(限制連線數,針對客戶端,即單一ip限流) - limit_req_zone(限制請求數,針對客戶端,即單一ip限流) - ngx_http_unpstream_module(推薦,針對後臺,如:有兩臺伺服器,伺服器A最大可併發處理10W條請求,伺服器B最大可併發處理5W條請求,這樣當12W請求按照負載均衡規則應當被分配給伺服器A時,nginx為了防止A掛掉,所以將另外的2W分配給B) ###### 壓力測試工具——Ab - 安裝 ``` yum install httpd-tools -y ``` - 測試 ``` // 10個使用者,向 http://www.test.com/ 併發傳送1000條請求(總請求數=1000) ab -c 10 -n 1000 http://www.test.com/ ``` - 返回值 - Document Path:測試的頁面路徑 - Document Length:頁面大小(byte) - Concurrency Level:併發數量,即併發使用者數 - Time taken for tests:測試耗費時長 - Complete requests:成功的請求數量 - Failed requests:請求失敗的數量 - Write errors:錯誤數量 - Requests per second:每秒鐘的請求數量、吞吐率 - Timer per request:每次請求所需時間、響應時間 ###### [limit_conn_zone](http://nginx.org/en/docs/http/ngx_http_limit_conn_module.html/) ``` http { # 將請求客戶端的IP($binary_remote_addr)存放到perip區域,區域大小為10M,一個IP佔用32Byte(32位系統)或64Byte(64位系統)左右 # perip是區域的名字,可自定義 limit_conn_zone $binary_remote_addr zone=perip:10m; server { # 每個IP最大併發1條連線 # 該語句還可直接放置到http模組下,這樣下屬的server都應用該配置 # 該語句還可放置到server中的location模組中,這樣僅指定的location應用該配置 limit_conn perip 1; # 每個連線限速300 k/s limit_rate 300k; } } ``` ###### [limit_req_zone](http://nginx.org/en/docs/http/ngx_http_limit_req_module.html) ``` http { # 將請求客戶端的IP存放到perip區域,區域大小為10M,並限制同一IP地址的請求每秒鐘只處理一次 limit_req_zone $binary_remote_addr zone=perip:10m rate=1r/s; server { # 當有大量請求爆發時,可以快取2條請求 # 設定了nodelay,快取佇列的請求會立即處理,若請求數 >
rate+burst 時,立即返回503;如果沒設定,則會按照rate排隊等待處理 # 該語句還可直接放置到http模組下,這樣下屬的server都應用該配置 # 該語句還可放置到server中的location模組中,這樣僅指定的location應用該配置 limit_req zone=perip burst=2 nodelay; } } ``` - 測試:`ab -c 1 -n 3 http://localhost/` - 3個請求全部成功,因為正在處理的請求數1加上快取數2,沒超過限制 ``` Server Software: nginx/1.18.0 Server Hostname: 192.168.159.128 Server Port: 80 Document Path: / Document Length: 612 bytes Concurrency Level: 1 Time taken for tests: 0.001 seconds Complete requests: 3 Failed requests: 0 Total transferred: 2535 bytes HTML transferred: 1836 bytes Requests per second: 2439.02 [#/sec] (mean) Time per request: 0.410 [ms] (mean) Time per request: 0.410 [ms] (mean, across all concurrent requests) Transfer rate: 2012.67 [Kbytes/sec] received ``` - 測試:`ab -c 3 -n 4 http://localhost/` - 3個請求成功,1個請求失敗,因為正在處理的請求數1加上快取數2,另外1條請求失敗 ``` erver Software: nginx/1.18.0 Server Hostname: 192.168.159.128 Server Port: 80 Document Path: / Document Length: 612 bytes Concurrency Level: 1 Time taken for tests: 0.002 seconds Complete requests: 4 Failed requests: 1 (Connect: 0, Receive: 0, Length: 1, Exceptions: 0) Non-2xx responses: 1 Total transferred: 3223 bytes HTML transferred: 2330 bytes Requests per second: 2504.70 [#/sec] (mean) Time per request: 0.399 [ms] (mean) Time per request: 0.399 [ms] (mean, across all concurrent requests) Transfer rate: 1970.86 [Kbytes/sec] received ``` ###### [ngx_http_upstream_module](http://nginx.org/en/docs/http/ngx_http_upstream_module.html) ``` upstream MyName { server 192.168.0.1:8080 weight=1 max_conns=10; server 192.168.0.2:8080 weight=1 max_conns=10; } ```
二、安全配置
###### 版本安全 - 隱藏HTTP Response訊息頭Server中的版本號 - 隱藏前:Server: nginx/1.18.0 - 隱藏後:Server: nginx ``` http { server_tokens off; } ``` ###### IP安全 - 白名單配置(適用於授權IP較少的情況),可配置在http、server、location中 ``` location / { allow 192.168.1.1; deny all; } ``` - 黑名單配置(適用於授權IP較多的情況),可配置在http、server、location中 ``` location / { deny 192.168.1.1; allow all; } ``` ###### 檔案安全 ``` location /logs { autoindex on; root/opt/nginx/; } location ^logs~*\.(log|txt)$ { add_header Content-Type text/plain; root/opt/nginx/; } ``` ###### 連線安全 - https(我就不多介紹了)
三、程序數、併發數、系統優化
###### 配置nginx.conf,增加併發量 ``` # 與CPU邏輯核心數一致 worker_processes 12; events { # 單個worker最大併發連線數 worker_connection 65535; } ``` ###### 調整核心引數 - 檢視所有的屬性值 ``` ulimit -a ``` - 臨時設定硬限制(重啟後失效) ``` ulimit -Hn 100000 ``` - 臨時設定軟限制(重啟後失效) ``` ulimit -Sn 100000 ``` - 持久化設定(重啟後仍生效) ``` vim /etc/security/limits.conf # 接下來是檔案中需要配置的內容 * soft nofile 100000 * hard nofile 100000 # 使用者/組 軟/硬限制 需要限制的專案 限制的值 ```
四、GZIP
- 作用:啟用gzip後,伺服器將響應報文進行壓縮,有效地節約了頻寬,提高了響應至客戶端的速度。當然,壓縮會消耗nginx所在電腦的cpu - 配置範圍:http、server、location ``` http { # 啟用gzip gzip on; # 允許壓縮的最小位元組數(即如果response header中的content-length小於該值,就不壓縮) gzip_min_length 2k; # 按照原資料大小以16k為單位的4倍申請記憶體用作壓縮快取 gzip_buffers 4 16k; # 壓縮級別,級別越大,壓縮率越高,佔用CPU時間更長 gzip_comp_level 5; # 需要被壓縮的響應型別,預設值是text/html gzip_types text/plain application/x-javascript text/css application/xml; # 配置最低版本的http壓縮協議(即1.0時,1.0和1.1都會啟用壓縮;1.1時,僅1.1時才會啟用壓縮) gzip_http_version 1.0; # IE6及以下禁用壓縮 gzip_disable "MSIE [1-6]\."; } ```
五、狀態監控
- 配置訪問地址 ``` location /nginxstatus { stub_status on; // 禁止將監控資訊寫入訪問日誌 access_log off; } ``` - 安裝外掛並重啟 ``` cd /usr/local/src/nginx-1.18.0 # 如果不是使用的預設路徑,使用 --prefix 指定 ./configure --with-http_stub_status_module make && make install /usr/local/nginx/sbin/nginx -s quit /usr/local/nginx/sbin/nginx ``` - 訪問地址,狀態引數如下 - Active connections:活躍的連線數量 - server accepts handled requests:處理的總連線數 建立的握手數 處理的總請求數 - reading:讀取客戶端的Header資訊的次數。這個操作僅讀取頭部資訊,完成後立即進入writing狀態 - writing:響應資料傳到客戶端的Header資訊次數。這個操作不僅讀取頭部,還要等待服務響應 - waiting:開啟keep-alive後等候下一次請求指令的駐留連線
六、負載均衡
###### 輪詢 ```nginx.conf upstream myserver { # 預設所有伺服器權重為 1 server 192.168.250.220:8080; server 192.168.250.221:8080; server 192.168.250.222:8080; } ``` ###### 加權輪詢 - 效能更好的伺服器權重應更高 ```nginx.conf upstream myserver { server 192.168.250.220:8080 weight=3; server 192.168.250.221:8080; # default weight=1 server 192.168.250.222:8080; # default weight=1 } ``` ###### 最少連線 ```nginx.conf upstream myserver { least_conn; # with default weight for all (weight=1) server 192.168.250.220:8080; server 192.168.250.221:8080; server 192.168.250.222:8080; } ``` ###### 加權最少連線 - 效能更好的伺服器權重應更高 ```nginx.conf upstream myserver { least_conn; server 192.168.250.220:8080 weight=3; server 192.168.250.221:8080; # default weight=1 server 192.168.250.222:8080; # default weight=1 } ``` ###### IP Hash - 演算法:根據客戶端ip進行Hash得到一個數值,然後使用該數值對伺服器個數取模,得到的結果就是對映的伺服器序號 - (在伺服器個數不變的情況下)可保證同一ip地址的請求始終對映到同一臺伺服器,解決了session共享問題 ```nginx.conf upstream myserver { ip_hash; # with default weight for all (weight=1) server 192.168.250.220:8080; server 192.168.250.221:8080; server 192.168.250.222:8080; } ``` ###### uri Hash - (在伺服器個數不變的情況下)可保證同一uri始終對映到同一臺伺服器 - nginx在1.7.2之後支援uri_hash ```nginx.conf upstream myserver { hash $request_uri; # with default weight for all (weight=1) server 192.168.250.220:8080; server 192.168.250.221:8080; server 192.168.250.222:8080; } ```
七、access日誌切割
- 新建Shell指令碼 ``` # 指令碼檔案路徑隨意 vim /usr/local/nginx/nginx_log.sh ``` - 將以下內容新增到指令碼中 ``` #! /bin/bash # 設定日誌檔案存放目錄(nginx安裝目錄為/usr/local/nginx) LOG_HOME="/usr/local/nginx/logs" # 備份Log名稱 LOG_PATH_BAK="$(date -d yesterday +%Y%m%d%H%M)".access.log # 重新命名日誌檔案 mv ${LOG_HOME}/access.log ${LOG_HOME}/${LOG_PATH_BAK}.log # 向nginx主程序發訊號重新開啟日誌 kill -USR1 `cat /usr/local/nginx/logs/nginx.pid` ``` - 建立crontab定時作業 ``` crontab -e ``` - 將以下內容新增到作業中去(任取一個) - corn表示式生成器:http://cron.qqe2.com/ ``` # 以每分鐘切割一次為例 */1 * * * * sh /usr/local/nginx/nginx_log.sh # 以每天切割一次為例 0 0 0 1/1 * ? sh /usr/local/nginx/nginx_log.sh ```
八、動靜分離
- 概念:將動態請求和靜態請求分開 - 實現方式: - (推薦)將靜態檔案存放在專門的伺服器上,使用單獨的域名 - 另一種是將動態和靜態檔案放在一起,使用nginx區分 ###### 以實現方式1為例 - 前提:將靜態檔案存放在代理伺服器中 - 在ngnix中建立檔案目錄(如/usr/local/nginx/static),將所有靜態檔案釋出到該目錄中 - 在nginx.conf http server 中配置動靜分離 ``` server { location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ { root /usr/local/nginx/static; # 快取30天 expires 30d; } } ``` - 在實際的後臺伺服器中釋出的程式中,使用靜態檔案時,路徑指向設定為靜態檔案伺服器(這裡是代理伺服器)

nginx.conf基礎配置項

``` # 指定執行nginx的使用者名稱 #user nobody; # 工作執行緒數,通常同cpu邏輯核心數一致 worker_processes 1; # 錯誤日誌路徑 最小級別 [ debug | info | notice | warn | error | crit ] #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; # 指定程序的pid記錄檔案,記錄當前執行的nginx的pid #pid logs/nginx.pid; # 網路連線模組 events { # 一個工作執行緒支援的最大併發連線數 worker_connections 1024; # keepalive超時時間,單位:秒 keepalive_timeout 60; } # 設定http伺服器,利用它的反向代理功能提供負載均衡支援 http { # 設定支援的 mime 型別 include mime.types; # 預設 mime 型別 default_type application/octet-stream; # 設定日誌格式,格式名為main ## $remote_addr:客戶端的ip地址(若使用代理伺服器,則是代理伺服器的ip) ## $remote_user:客戶端的使用者名稱(一般為“-”) ## $time_local:訪問時間和時區 ## $request:請求的url和請求方法 ## $status:響應HTTP狀態碼 ## $body_bytes_sent:響應body中的位元組數 ## $http_referer:客戶端是從哪個url來請求的 ## $http_user_agent:客戶端使用者使用的代理(一般為瀏覽器) ## $http_x_forwarded_for:客戶端的ip地址(通過代理伺服器記錄客戶端的ip地址) #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; # 指定 nginx 是否呼叫 sendfile 函式(zero copy 方式)來輸出檔案,對於普通應用,必須設為 on, # 如果用來進行下載等應用磁碟IO重負載應用,可設定為 off,以平衡磁碟與網路I/O處理速度,降低系統的uptime sendfile on; #tcp_nopush on; # keepalive 超時時長,單位:秒 #keepalive_timeout 0; keepalive_timeout 65; # 開啟 gzip #gzip on; # 以上為 nginx 的全域性設定,應用於所有 Web 應用 # 一個Web應用對應一個 server,內部配置僅針對該應用,優先順序比全域性的高 server { // 埠號 listen 80; // 域名,比如 www.test.com server_name localhost; # 編碼格式 #charset koi8-r; # 訪問日誌檔案路徑 #access_log logs/host.access.log main; # 一般路由導航到: location / { # 根目錄為html root html; # 預設頁為 index.html,如果沒有則是 index.htm index index.html index.htm; } # 404時的展示頁面 #error_page 404 /404.html; # 50X時的展示頁面 # 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; #} # 禁止訪問 .htxxx 的檔案 # 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 demo beign ############## # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl 證書檔案位置 # ssl_certificate cert.pem; # ssl 證書key的位置 # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # 數字簽名 MD5 # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} ############### HTTPS demo end ############### ############ 反向代理 demo begin ############# # 設定實際的伺服器列表(權重預設都是1) #upstream myserver{ # server 192.168.0.1:8089 weight=7; # server 192.168.0.2:8089 weight=3; #} #server { # listen 80; # server_name localhost; #反向代理的路徑(和upstream繫結),location 後面設定對映的路徑 # location / { # proxy_pass http://myserver; # } #} ############# 反向代理 demo end ##############