一、Nginx簡介
Nginx是一款輕量級的反向代理服務器,也是一款輕量級的Web服務器(較少用到)
1.1、Nginx的使用場景
1、作為http反向代理服務器
2、作為負載均衡服務器
3、直接支持php(web服務器)
4、作為郵件代理服務器
5、幫助實作前端動靜分離
…
1.2、Nginx特點
高穩定、高性能、資源占用少、功能豐富、模塊化結構、支持熱部署
單臺NG可支撐10萬并發
二、Nginx的安裝
2.1、安裝
以mac安裝為例:
Step1:安裝brew
/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"
Step2:安裝Nginx
brew install nginx
2.2、安裝后測驗
1、測驗配置是否正常
/nginx/sbin/nginx -t
2、啟動nginx
/nginx/sbin/nginx
3、停止nginx
/nginx/sbin/nginx -s stop
4、重啟nginx
/nginx/sbin/nginx -s reload
5、查看行程
ps -ef | grep nginx
三、Nginx的基本配置
組態檔在:安裝路徑/conf/nginx.conf
常見的組態檔:
1)nginx.conf:應用程式的基本組態檔
2)mime.types:MIME型別關聯的擴展檔案
3)fastcgi.conf:與fastcgi相關的配置(php開發時用)
4)proxy.conf:與proxy相關的配置(也可以在nginx.conf中配置)
5)sites.conf:配置Nginx提供的網站,包括虛擬主機(也可以在nginx.conf中配置)
Nginx的行程結構:
啟動nginx時,會啟動一個master行程,這個行程不處理任何客戶端請求,主要用來產生worker行程,一個worker行程用來處理一個request,
這個方式類似于epoll reactor模型,
這個配置就是用于指定每個行程可啟動的worker數,默認1024
events {
use epoll;
worker_connections 1024;
}
http模塊用來指定web的反向代理
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;
...
}
log_format指定日志格式,$后面表示變數
access_log指定nginx的訪問日志放到哪
從上面可以看出,nginx是模塊化的方式,每個模塊負責一個功能,
nginx的請求來了,可以想象成每個模塊逐個走一遍,就好比責任鏈每個節點逐個過一遍,所以nginx的擴展性非常好:只要增減模塊就行了,要增減模塊,就是通過在組態檔中配置
upstream是用來做負載均衡:
upstream test1.com{
server 127.0.0.1:8080 weight=5;
server 127.0.0.1:1111;
}
server:后端的虛擬主機
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://test1.com; # 指向upstream
}
}
其中,proxy_pass指nginx反向代理后指向的目標地址
nginx是由多個模塊組成,所以要學習每個模塊的配置使用,這些配置就類似于現成的API,使用API的方法就是我們去配置,
Nginx的模塊包括:核心模塊、事件模塊、標準http模塊、可選http模塊、郵件模塊、第三方模塊、nginx自身的補丁等等
3.1、基本模塊
基本模塊指nginx默認的功能模塊,他們提供的指令,允許你使用定義nginx基本功能的變數,
在編譯的時候不能被禁用,包括:
1)核心模塊:基本功能和指令,如行程管理和安全
2)事件模塊:在nginx內配置網路使用的能力
3)配置模塊:提供include機制
至于nginx中都支持哪些配置,每個配置干什么用的,可參看nginx官方檔案:https://nginx.org/en/docs/
3.1.1、error_log
Syntax: error_log file [level];
Default: error_log logs/error.log error;
Context: main, http, mail, stream, server, location
指定錯誤檔案的位置及日志等級
每個server{}可以有自己的error_log地址
3.1.2、include
Syntax: include file | mask;
Default: —
Context: any
從外部引入檔案,可以支持檔案的通配符
3.1.3、pid
Syntax: pid file;
Default:
pid logs/nginx.pid;
Context: main
Defines a file that will store the process ID of the main process.
默認配置就有:
pid logs/nginx.pid;
3.1.4、user
Syntax: user user [group];
Default:
user nobody nobody;
Context: main
Defines user and group credentials used by worker processes. If group is omitted, a group whose name equals that of user is used.
指誰在運行nginx,通常用root運行,但要提升安全性,可以單獨指定可運行nginx的用戶,
默認配置:
user nobody;
3.1.5、worker_cpu_affinity
Syntax: worker_cpu_affinity cpumask ...;
worker_cpu_affinity auto [cpumask];
Default: —
Context: main
Binds worker processes to the sets of CPUs. Each CPU set is represented by a bitmask of allowed CPUs. There should be a separate set defined for each of the worker processes. By default, worker processes are not bound to any specific CPUs.
如果服務器上有多個CPU,可以指定worker行程和CPU的系結關系
For example,
worker_processes 4;
worker_cpu_affinity 0001 0010 0100 1000;
binds each worker process to a separate CPU, while
worker_processes 2;
worker_cpu_affinity 0101 1010;
binds the first worker process to CPU0/CPU2, and the second worker process to CPU1/CPU3. The second example is suitable for hyper-threading.
通常沒必要配置這個,用默認就可以
3.1.6、worker_processes
Syntax: worker_processes number | auto;
Default:
worker_processes 1;
Context: main
Defines the number of worker processes.
通常worker_processes是CPU核數的整數倍
3.2、日志模塊
日志模塊是控制nginx如何記錄請求的日志
log_format compression '$remote_addr - $remote_user [$time_local] '
'"$request" $status $bytes_sent '
'"$http_referer" "$http_user_agent" "$gzip_ratio"';
access_log /spool/logs/nginx-access.log compression buffer=32k;
Syntax: access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]];
access_log off;
Default:
access_log logs/access.log combined;
Context: http, server, location, if in location, limit_except
緩沖區大小不能超過寫入檔案的最小大小
nginx用戶必須有寫入nginx日志檔案的權限
3.3、事件模塊
推薦配置:
worker_processes 8 : 這里的8,推薦跟你的CPU核數相同
events {
use epoll;
worker_connections 1024; # 這里跟你作業系統能打開的檔案句柄數相同, 可通過ulimit -n來查看
}
3.4、HTTP核心模塊
http配置主要分三個模塊:
http { # 這個是協議級別
include mime.types;
default_type application/octet-stream;
keepalive_timeout 65;
gzip on
server { # 這個是服務器級別,可以配置多個server
listen 80;
server_name localhost;
location ~* .~\.(jpg|jpeg|gif|png|swf|ico)$ {
if (-f $request_filename) {
break;
}
}
location / { # 這個是請求級別
root html
index index.htm index.htm
}
}
}
Nginx的http核心魔,包括大量指令和變數,
3.4.1、alias
Syntax: alias path;
Default: —
Context: location
Defines a replacement for the specified location. For example, with the following configuration
location /i/ {
alias /data/w3/images/;
}
nginx會把 /i/ 自動替換為 /data/w3/images/
也支持正則
3.4.2、client_max_body_size
Syntax: client_max_body_size size;
Default:
client_max_body_size 1m;
Context: http, server, location
Sets the maximum allowed size of the client request body. If the size in a request exceeds the configured value, the 413 (Request Entity Too Large) error is returned to the client. Please be aware that browsers cannot correctly display this error. Setting size to 0 disables checking of client request body size.
3.4.3、error_page
Syntax: error_page code ... [=[response]] uri;
Default: —
Context: http, server, location, if in location
Defines the URI that will be shown for the specified errors. A uri value can contain variables.
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
如果出現404,則跳轉到/404.html
3.4.4、listen
指定server里可以被訪問到的IP和埠地址,或可決議到的服務器的沒名字,通常我們都只指定埠就可以,
Syntax: listen address[:port] [default_server] [ssl] [http2 | spdy] [proxy_protocol] [setfib=number] [fastopen=number] [backlog=number] [rcvbuf=size] [sndbuf=size] [accept_filter=filter] [deferred] [bind] [ipv6only=on|off] [reuseport] [so_keepalive=on|off|[keepidle]:[keepintvl]:[keepcnt]];
listen port [default_server] [ssl] [http2 | spdy] [proxy_protocol] [setfib=number] [fastopen=number] [backlog=number] [rcvbuf=size] [sndbuf=size] [accept_filter=filter] [deferred] [bind] [ipv6only=on|off] [reuseport] [so_keepalive=on|off|[keepidle]:[keepintvl]:[keepcnt]];
listen unix:path [default_server] [ssl] [http2 | spdy] [proxy_protocol] [backlog=number] [rcvbuf=size] [sndbuf=size] [accept_filter=filter] [deferred] [bind] [so_keepalive=on|off|[keepidle]:[keepintvl]:[keepcnt]];
Default:
listen *:80 | *:8000;
Context: server
3.4.5、open_file_cache
open_file_cache max=1000 inactive=20s; #設定快取的最大資料,過期時間
open_file_cache_valid 30s; # 有效時間
open_file_cache_min_uses 2; # 最少使用此時
open_file_cache_errors on; # 通常打開
這幾個配置配合使用
3.4.6、Location
Syntax: location [ = | ~ | ~* | ^~ ] uri { ... }
location @name { ... }
Default: —
Context: server, location
指定模式來與客戶端請求的URI 相匹配,
1、沒有修飾符,表示:必須從指定模式開始,如:
server {
server_name test.com
location /abc {
...
}
}
2、 = 表示:精確匹配,如:
server {
server_name test.com
location = /abc {
...
}
}
3、~ 表示:指定的正則運算式要區分大小寫,如:
server {
server_name test.com
location ~ ^/abc {
...
}
}
下面是對的:
http://test.com/abc
http://test.com/abc?p1=1
下面是錯的:
http://test.com/ABC
http://test.com/abc/
4、~* 表示:指定的正則運算式不區分大小寫,如:
server {
server_name test.com
location ~* ^/abc {
...
}
}
下面是對的:
http://test.com/abc
http://test.com/abc?p1=1
http://test.com/ABC
下面是錯的:
http://test.com/abc/
5、^~ 表示無修飾符的行為,也是從指定模式開始,不同的是,如果模式匹配,那么久停止搜索其他模式了
6、@ 定義命名location區段,這些區段客戶端不能訪問,只可以由內部產生的請求來訪問,如try_file或error_page等
查找順序和優先級:
1)帶有 = 的精確匹配
2)沒有修飾符的精確匹配
3)正則運算式安裝他們在組態檔中定義的順序
4)帶有 ~ 修飾符的,開頭匹配
5)帶有 ~ 或 ~* 修飾符的
6)沒有修飾符的,如指定字串與URI開頭匹配,如: location / {…}, 這樣配置用來兜底,優先級最低
3.5、HTTP反向代理模塊
nginx通常用于后端服務器的反向代理,這樣可方便的實作動靜分離,以及負載均衡
http proxy模塊,功能很多,最常用的時proxy_pass
http proxy模塊,主要用于轉發請求到其他服務器
location / {
proxy_pass http://localhost:8000; # 轉發指令
proxy_set_header Host $host; # 設定header的值
proxy_set_header X-Real-IP $remote_addr; # 設定IP地址
}
3.5.1、proxy_buffer_size&proxy_buffering&proxy_buffers
Syntax: proxy_buffer_size size;
Default:
proxy_buffer_size 4k|8k;
Context: http, server, location
Syntax: proxy_buffering on | off;
Default:
proxy_buffering on;
Context: http, server, location
Syntax: proxy_buffers number size;
Default:
proxy_buffers 8 4k|8k;
Context: http, server, location
proxy_buffer_size: 設定從被代理服務器讀取的第一部分應答的緩沖區大小
即從后端tomcat回傳應答的緩沖區大小,默認是4k/8k
proxy_buffering:為后端服務器啟用應答快取
如果啟用快取,nginx假設被代理服務器能夠非常快的傳遞應答,并將其放入緩沖區,可以使用proxy_buffer_size和proxy_buffers設定相關引數
proxy_buffers:設定用于讀取應答(來自被代理服務器)的緩沖區數目和大小,默認情況為分頁大小,根據作業系統不同可能是4k或8k
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/304880.html
標籤:其他
上一篇:如何啟動 WordPress 博客 – 簡易指南 – 創建博客(2021)
下一篇:Web 基礎與 HTTP 協議
