做者 | WenasWeijavascript
Nginx 是一款高性能的 HTTP 服務器/反向代理服務器及 IMAP/POP3/SMTP 代理服務器。由俄羅斯的程序設計師 Igor Sysoev 所開發,官方測試 Nginx 可以支支撐 5 萬併發連接,而且 CPU、內存等資源消耗卻很是低,運行很是穩定。css
Nginx 代碼徹底用C語言從頭寫成,以事件驅動的方式編寫,因此有很是好的性能,同時也是一個很是高效的反向代理、負載平衡服務器。在性能上,Nginx佔用不多的系統資源,能支持更多的併發鏈接,達到更高的訪問效率;在功能上,Nginx是優秀的代理服務器和負載均衡服務器;在安裝配置上,Nginx安裝簡單、配置靈活。html
截至2019年12月,差很少世界上每3個網站中就有1個使用 Nginx。前端
Nginx支持熱部署,啓動速度特別快,還能夠在不間斷服務的狀況下對軟件版本或配置進行升級,即便運行數月也無需從新啓動。java
在微服務的體系之下,Nginx正在被愈來愈多的項目採用做爲網關來使用,配合Lua作限流、熔斷等控制。linux
nginx 的使用比較簡單,就是幾條命令。nginx
nginx -s stop #快速關閉Nginx,可能不保存相關信息,並迅速終止web服務。 nginx -s quit #平穩關閉Nginx,保存相關信息,有安排的結束web服務。 nginx -s reload #因改變了Nginx相關配置,須要從新加載配置而重載。 nginx -s reopen #從新打開日誌文件。 nginx -c filename #爲 Nginx 指定一個配置文件,來代替缺省的。 nginx -t #不運行,而僅僅測試配置文件。nginx將檢查配置文件的語法的正確性,並嘗試打開配置文件中所引用到的文件。 nginx -v #顯示 nginx 的版本。 nginx -V #顯示 nginx 的版本,編譯器版本和配置參數。
在linux的sbin目錄下中輸入:./nginx
代替上面的 nginx
程序員
若是不想每次都敲命令,能夠在 nginx 安裝目錄下新添一個啓動批處理文件 startup.bat,雙擊便可運行。內容以下:web
驗證Nginx配置文件是否正確docker
nginx -t -c /conf/nginx.conf
@echo off #rem 若是啓動前已經啓動nginx並記錄下pid文件,會kill指定進程 nginx -s stop #rem 測試配置文件語法正確性 nginx -t -c nginx.conf #rem 顯示版本信息 nginx -v #rem 按照指定配置去啓動nginx nginx -c nginx.conf
windows 下其餘經常使用命令
nginx.exe -s stop #快速關閉Nginx,可能不保存相關信息,並迅速終止web服務。 nginx.exe -s quit #平穩關閉Nginx,保存相關信息,有安排的結束web服務。 nginx.exe -s reload #因改變了Nginx相關配置,須要從新加載配置而重載。 nginx.exe -s reopen #從新打開日誌文件。 nginx.exe -c filename #爲 Nginx 指定一個配置文件,來代替缺省的。 nginx.exe -t #不運行,而僅僅測試配置文件。nginx將檢查配置文件的語法的正確性,並嘗試打開配置文件中所引用到的文件。 nginx.exe -v #顯示 nginx 的版本。 nginx.exe -V #顯示 nginx 的版本,編譯器版本和配置參數。
cmd 命令窗口查看 Nginx 啓動命令
tasklist /fi "imagename eq nginx.exe"
下圖結果爲啓動成功
驗證Nginx配置文件是否正確
nginx.exe -t -c \conf\nginx.conf
下面將介紹 Nginx 的三種部署使用案例:
最新版本nginx-1.9.9,下載地址:
http://nginx.org/download/nginx-1.9.9.zip
下載後解壓,解壓後以下
有不少種方法啓動nginx
nginx.exe
或者 start nginx
,回車便可注意:
啓動前能夠看到Nginx默認使用啓動監聽端口爲 80, 須要檢查 80 端口是否被佔用,可以使用命令:
netstat -ano | findstr 0.0.0.0:80
netstat -ano | findstr "80"
如若須要自定義其餘端口也須要檢查端口是否被佔用,修改配置文件啓動前也能夠進行配置文件檢查是否配置正常,檢查命令:
nginx.exe -t -c \conf\nginx.conf
檢查結果以下圖所示:
當咱們修改了nginx 的配置文件 nginx.conf 時,不須要關閉 nginx 後從新啓動 nginx,只須要執行命令 nginx -s reload
便可讓改動生效
直接在瀏覽器地址欄輸入網址 http://localhost:80,回車,出現如下頁面說明啓動成功
404test
測試文件夾,添加測試 index.html,html內容頁面代碼地址:以下兩張圖所示:
新增修改配置文件: \conf\nginx.conf
server { listen 80; server_name localhost; location / { root C:/Users/wenas/Downloads/nginx-1.9.9/conf/; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
訪問結果如圖所示:
添加轉發百度頁面地址
server { listen 80; server_name localhost; # 配置建立的 testindex.html 目錄地址 location / { root C:/Users/wenas/Downloads/nginx-1.9.9/404test/; index index.html index.htm; } location /baidu { proxy_pass http://www.baidu.com/; proxy_connect_timeout 10s; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
訪問結果如圖所示:
能夠看到url地址爲代理的百度地址,內容數據由百度的接口提供。
若是使用cmd命令窗口啓動nginx,關閉 cmd 窗口是不能結束nginx進程的,可以使用兩種方法關閉 nginx
nginx -s stop
(快速中止nginx) 或 nginx -s quit
(完整有序的中止nginx)taskkill /f /t /im nginx.exe
使用 lsb_release -a
查看到 Linux 系統爲 Ubuntu 18.04.2 LTS
$ lsb_release -a LSB Version: core-9.20170808ubuntu1-noarch:security-9.20170808ubuntu1-noarch Distributor ID: Ubuntu Description: Ubuntu 18.04.2 LTS Release: 18.04 Codename: bionic
$ vi /etc/apt/sources.list
sources.list
全部的文件內容,可使用 echo > sources.lis
一鍵清空deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse
$ apt-get update
# 切換至root用戶 $ sudo su root
Linux 部署 Nginx 目前支持兩種安裝方式, 一種是 apt-get 的方式, 另外一種是根據包安裝的方式。
$ apt-get install nginx
$ nginx -v nginx version: nginx/1.10.3 (Ubuntu)
Nginx文件安裝完成以後的文件位置
進入到配置文件地址修改配置文件 nginx.conf
:
$ cd /etc/nginx $ ll total 64 drwxr-xr-x 6 root root 4096 Apr 26 23:55 ./ drwxr-xr-x 94 root root 4096 Apr 25 15:40 ../ drwxr-xr-x 2 root root 4096 Jan 11 2020 conf.d/ -rw-r--r-- 1 root root 1077 Feb 12 2017 fastcgi.conf -rw-r--r-- 1 root root 1007 Feb 12 2017 fastcgi_params -rw-r--r-- 1 root root 2837 Feb 12 2017 koi-utf -rw-r--r-- 1 root root 2223 Feb 12 2017 koi-win -rw-r--r-- 1 root root 3957 Feb 12 2017 mime.types -rw-r--r-- 1 root root 2959 Apr 26 23:55 nginx.conf -rw-r--r-- 1 root root 180 Feb 12 2017 proxy_params -rw-r--r-- 1 root root 636 Feb 12 2017 scgi_params drwxr-xr-x 2 root root 4096 Apr 25 15:40 sites-available/ drwxr-xr-x 2 root root 4096 Apr 25 15:40 sites-enabled/ drwxr-xr-x 2 root root 4096 Apr 25 15:40 snippets/ -rw-r--r-- 1 root root 664 Feb 12 2017 uwsgi_params -rw-r--r-- 1 root root 3071 Feb 12 2017 win-utf
修改配置文件以下:
#user nobody; worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 7070; server_name localhost; location / { # 上傳程序員404-index.html到該指定目錄下 root /usr/share/nginx/html/; index index.html index.htm; } } }
驗證配置文件是否配置正確
$ nginx -t -c /etc/nginx/nginx.conf
驗證結果以下:
$ nginx -t -c /etc/nginx/nginx.conf nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
啓動Nginx指令:
service nginx start
查看Nginx啓動進程:
$ ps -ef|grep nginx root 1980 1 0 20:14 ? 00:00:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on; nobody 1983 1980 0 20:14 ? 00:00:00 nginx: worker process root 2051 11018 0 20:14 pts/0 00:00:00 grep --color=auto nginx root 25087 25065 0 Apr25 ? 00:00:00 nginx: master process nginx -g daemon off; systemd+ 25160 25087 0 Apr25 ? 00:00:00 nginx: worker process
在瀏覽器面上打開URL: ip:7070
就能看到咱們可愛的程序猿404了
# 完全卸載nginx $ apt-get --purge autoremove nginx #查看nginx的版本號 $ nginx -v
apt-get install gcc apt-get install libpcre3 libpcre3-dev apt-get install zlib1g zlib1g-dev # Ubuntu14.04的倉庫中沒有發現openssl-dev,由下面openssl和libssl-dev替代 #apt-get install openssl openssl-dev sudo apt-get install openssl sudo apt-get install libssl-dev
cd /usr/local mkdir nginx cd nginx wget http://nginx.org/download/nginx-1.13.7.tar.gz tar -xvf nginx-1.13.7.tar.gz
# 進入nginx目錄 /usr/local/nginx/nginx-1.13.7 # 執行命令 ./configure # 執行make命令 make # 執行make install命令 make install
#進入nginx啓動目錄 cd /usr/local/nginx/sbin # 啓動nginx ./nginx
訪問nginx
網頁輸入ip地址: 開放端口,訪問成功,到此,nginx安裝完畢
本篇部署須要使用到 Docker 容器化引擎技術部署,須要在 Linux 上安裝 Docker環境與Docker-compose環境
$ docker -v Docker version 18.09.7, build 2d0083d $ docker-compose -v docker-compose version 1.25.0-rc4, build 8f3c9c58
docker pull nginx
操做文件目錄爲: /usr/local/docker/nginx
文件目錄爲:
- conf/ - nginx.conf - docker-compose.yml
(1)docker-compose.yml配置文件
version: '3.1' services: nginx: restart: always image: nginx container_name: nginx ports: - 8081:80 volumes: - ./conf/nginx.conf:/etc/nginx/nginx.conf - /home/wenas/html/:/home/wenas/html/
(2)nginx.conf配置文件
user nginx; worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; # 配置一個虛擬主機 server { listen 80; location /testhtml/ { alias /home/wenas/html/; autoindex on; } } }
啓動命令
$ docker-compose up -d Starting nginx ... done
查看啓動的Nginx容器
$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 8141bae75054 nginx "nginx -g 'daemon of…" 12 days ago Up 26 seconds 0.0.0.0:8081->80/tcp nginx
訪問URL : IP:8081/testhtml/
網站在實際運營過程當中,多半都是有多臺服務器運行着一樣的app,這時須要使用負載均衡來分流。
nginx也能夠實現簡單的負載均衡功能。
假設這樣一個應用場景:將應用部署在 192.168.1.11:80、192.168.1.12:80、192.168.1.13:80 三臺linux環境的服務器上。網站域名叫 www.helloworld.com,公網IP爲 192.168.1.11。在公網IP所在的服務器上部署 nginx,對全部請求作負載均衡處理。
nginx.conf 配置以下:
http { #設定mime類型,類型由mime.type文件定義 include /etc/nginx/mime.types; default_type application/octet-stream; #設定日誌格式 access_log /var/log/nginx/access.log; #設定負載均衡的服務器列表 upstream load_balance_server { #weigth參數表示權值,權值越高被分配到的概率越大 server 192.168.1.11:80 weight=5; server 192.168.1.12:80 weight=1; server 192.168.1.13:80 weight=6; } #HTTP服務器 server { #偵聽80端口 listen 80; #定義使用www.xx.com訪問 server_name www.helloworld.com; #對全部請求進行負載均衡請求 location / { root /root; #定義服務器的默認網站根目錄位置 index index.html index.htm; #定義首頁索引文件的名稱 proxy_pass http://load_balance_server ;#請求轉向load_balance_server 定義的服務器列表 #如下是一些反向代理的配置(可選擇性配置) #proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; #後端的Web服務器能夠經過X-Forwarded-For獲取用戶真實IP proxy_set_header X-Forwarded-For $remote_addr; proxy_connect_timeout 90; #nginx跟後端服務器鏈接超時時間(代理鏈接超時) proxy_send_timeout 90; #後端服務器數據回傳時間(代理髮送超時) proxy_read_timeout 90; #鏈接成功後,後端服務器響應時間(代理接收超時) proxy_buffer_size 4k; #設置代理服務器(nginx)保存用戶頭信息的緩衝區大小 proxy_buffers 4 32k; #proxy_buffers緩衝區,網頁平均在32k如下的話,這樣設置 proxy_busy_buffers_size 64k; #高負荷下緩衝大小(proxy_buffers*2) proxy_temp_file_write_size 64k; #設定緩存文件夾大小,大於這個值,將從upstream服務器傳 client_max_body_size 10m; #容許客戶端請求的最大單文件字節數 client_body_buffer_size 128k; #緩衝區代理緩衝用戶端請求的最大字節數 } } }
當一個網站功能愈來愈豐富時,每每須要將一些功能相對獨立的模塊剝離出來,獨立維護。這樣的話,一般,會有多個 webapp。
舉個例子:假如 www.helloworld.com 站點有好幾個webapp,finance(金融)、product(產品)、admin(用戶中心)。訪問這些應用的方式經過上下文(context)來進行區分:
www.helloworld.com/finance/
www.helloworld.com/product/
www.helloworld.com/admin/
咱們知道,http的默認端口號是80,若是在一臺服務器上同時啓動這3個 webapp 應用,都用80端口,確定是不成的。因此,這三個應用須要分別綁定不一樣的端口號。
那麼,問題來了,用戶在實際訪問 www.helloworld.com 站點時,訪問不一樣 webapp,總不會還帶着對應的端口號去訪問吧。因此,你再次須要用到反向代理來作處理。
配置也不難,來看看怎麼作吧:
http { #此處省略一些基本配置 upstream product_server{ server www.helloworld.com:8081; } upstream admin_server{ server www.helloworld.com:8082; } upstream finance_server{ server www.helloworld.com:8083; } server { #此處省略一些基本配置 #默認指向product的server location / { proxy_pass http://product_server; } location /product/{ proxy_pass http://product_server; } location /admin/ { proxy_pass http://admin_server; } location /finance/ { proxy_pass http://finance_server; } } }
一些對安全性要求比較高的站點,可能會使用 HTTPS(一種使用ssl通訊標準的安全HTTP協議)。
這裏不科普 HTTP 協議和 SSL 標準。可是,使用 nginx 配置 https 須要知道幾點:
#HTTP服務器 server { #監聽443端口。443爲知名端口號,主要用於HTTPS協議 listen 443 ssl; #定義使用www.xx.com訪問 server_name www.helloworld.com; #ssl證書文件位置(常見證書文件格式爲:crt/pem) ssl_certificate cert.pem; #ssl證書key位置 ssl_certificate_key cert.key; #ssl配置參數(選擇性配置) ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m; #數字簽名,此處使用MD5 ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; location / { root /root; index index.html index.htm; } }
有時候,咱們須要配置靜態站點(即 html 文件和一堆靜態資源)。
舉例來講:若是全部的靜態資源都放在了 /app/dist 目錄下,咱們只須要在 nginx.conf 中指定首頁以及這個站點的 host 便可。
配置以下:
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; gzip on; gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/javascript image/jpeg image/gif image/png; gzip_vary on; server { listen 80; server_name static.zp.cn; location / { root /app/dist; index index.html; #轉發任何請求到 index.html } } }
而後,添加 HOST:
127.0.0.1 static.zp.cn
此時,在本地瀏覽器訪問 static.zp.cn ,就能夠訪問靜態站點了。
web 領域開發中,常常採用先後端分離模式。這種模式下,前端和後端分別是獨立的 web 應用程序,例如:後端是 Java 程序,前端是 React 或 Vue 應用。
各自獨立的 web app 在互相訪問時,勢必存在跨域問題。解決跨域問題通常有兩種思路:
在後端服務器設置 HTTP 響應頭,把你須要運行訪問的域名加入加入 Access-Control-Allow-Origin 中。
把後端根據請求,構造json數據,並返回,前端用 jsonp 跨域。
這兩種思路,本文不展開討論。
須要說明的是,nginx 根據第一種思路,也提供了一種解決跨域的解決方案。
舉例:www.helloworld.com 網站是由一個前端 app ,一個後端 app 組成的。前端端口號爲 9000, 後端端口號爲 8080。
前端和後端若是使用 http 進行交互時,請求會被拒絕,由於存在跨域問題。來看看,nginx 是怎麼解決的吧:
首先,在 enable-cors.conf 文件中設置 cors :
# allow origin list set $ACAO '*'; # set single origin if ($http_origin ~* (www.helloworld.com)$) { set $ACAO $http_origin; } if ($cors = "trueget") { add_header 'Access-Control-Allow-Origin' "$http_origin"; add_header 'Access-Control-Allow-Credentials' 'true'; add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type'; } if ($request_method = 'OPTIONS') { set $cors "${cors}options"; } if ($request_method = 'GET') { set $cors "${cors}get"; } if ($request_method = 'POST') { set $cors "${cors}post"; }
接下來,在你的服務器中 include enable-cors.conf 來引入跨域配置:
# ---------------------------------------------------- # 此文件爲項目 nginx 配置片斷 # 能夠直接在 nginx config 中 include(推薦) # 或者 copy 到現有 nginx 中,自行配置 # www.helloworld.com 域名需配合 dns hosts 進行配置 # 其中,api 開啓了 cors,需配合本目錄下另外一份配置文件 # ---------------------------------------------------- upstream front_server{ server www.helloworld.com:9000; } upstream api_server{ server www.helloworld.com:8080; } server { listen 80; server_name www.helloworld.com; location ~ ^/api/ { include enable-cors.conf; proxy_pass http://api_server; rewrite "^/api/(.*)$" /$1 break; } location ~ ^/ { proxy_pass http://front_server; } }