簡介
Nginx 的安裝就不解釋了,方便起見,建議在各平臺可以直接執行對應安裝命令:
yum install nginx;
sudo apt-get install nginx;
brew install nginx;
|
一般可以在 /etc/nginx/nginx.conf
中配置,啓動參數爲:
nginx -s start;
nginx -s reload;
nginx -s stop;
nginx -t;
|
-s
,signal,意思就是向 nginx 發送 start|reload|stop
命令,還是很好理解的。先看一個最簡單的 nginx.conf
配置:
events { } http { server { listen 127.0.0.1:8888; location / { root /home/barret/test/; } } }
|
啓動後,訪問 htttp://127.0.0.1:8888 ,如果 /home/barret/test/
下有 index.html
文件就會展示 index.html
的內容,否則返回 404
。
Nginx 配置一個 Web 服務器
以下對配置 Web 服務器的參數做簡單說明,包括如何配置端口、域名,如何處理請求,如何響應請求。
1、 虛擬主機和請求的分發
域名和端口的配置
listen 127.0.0.1:8000; listen *:8000; listen localhost:8000;
listen [::]:8000;
listen 443 default_serer ssl; listen 127.0.0.1 default_server accept_filter=dataready backlog=1024
|
主機名配置
server_name www.barretlee.com barretlee.com server_name *.barretlee.com server_name ~^\.barret\.com$
|
URI 匹配
location = / { } location ^~ /images/ { } location / { }
|
2、 文件路徑的定義
根目錄設置
location / { root /home/barret/test/; }
|
別名設置
location /blog { alias /home/barret/www/blog/; } location ~ ^/blog/(\d+)/([\w-]+)$ { alias /home/barret/www/blog/$1-$2.md; }
|
首頁設置
index /html/index.html /php/index.php;
|
重定向頁面設置
error_page 404 /404.html; error_page 502 503 /50x.html; error_page 404 =200 /1x1.gif;
location / { error_page 404 @fallback; } location @fallback { proxy_pass http://localhost:9000; }
|
try_files 設置
try_files $uri $uri.html $uri/index.html @other; location @other { proxy_pass http://localhost:9000; } location / { try_files $uri $uri.html =502; }
|
Nginx 配置反向代理服務器
反向代理(reserve proxy)方式是指用代理服務器來接受 Internet 上的連接請求,然後將請求轉發給內部網絡中的上游服務器,並將上游服務器上得到的結果返回給 Internet 上請求連接的客戶端,此時代理服務器對外的表現就是一個 Web 服務器。
Nginx 具備超強的高併發高負載能力,一般會作爲前端的服務器直接向客戶端提供靜態文件服務;而業務一般還包含一些業務邏輯需要 Apache、Tomcat 等服務器來處理,故通常 Nginx 對外表現即爲靜態 Web 服務器也是反向代理服務器。
缺點是增加了一次請求的處理時間,優點是降低了上游服務器的負載,儘量將壓力放在 Nginx 服務器上。
1、負載均衡配置
upstream,定義一個上游服務器集羣
upstream backend { server s1.barretlee.com; server s2.barretlee.com; } server { location / { proxy_pass http://backend; } }
|
2、反向代理
proxy_pass 將請求轉發到有處理能力的端上,默認不會轉發請求中的 Host 頭部
location /blog { prox_pass http://localhost:9000;
proxy_set_header Host $host; proxy_method POST; proxy_hide_header Cache-Control; proxy_hide_header Other-Header; proxy_pass_header Server-IP; proxy_pass_header Server-Name; proxy_pass_request_body on | off; proxy_pass_request_headers on | off; proxy_redirect on | off; }
|
一個簡單的例子,Node.js
一個十分常見的需求:處理請求,如果是靜態文件,Nginx 直接返回,否則交給 Node 服務器處理。首先創建了一個 Node 服務器:
const http = require('http'); http.createServer((req, res) => { res.end('hello world'); }).listen(9000);
|
任何請求過來都返回 hello world
,簡版的 Nginx 配置如下,
events { use epoll; } http { server { listen 127.0.0.1:8888; try_files $uri $uri/index.html; location ~* ^/(js|css|image|font)/$ { root /home/barret/www/static/; } location /app { proxy_pass http://127.0.0.1:9000; } error_page 404 /404.html; error_page 502 503 504 /50x.html; } }
|
首先 try_files,嘗試直接匹配文件;沒找到就匹配靜態資源;還沒找到就交給 Node 處理;否則就返回 4xx/5xx 的狀態碼。
原文轉載至:https://www.barretlee.com/blog/2016/11/19/nginx-configuration-start/