Nginx 入門(一)Nginx 配置Web服務器
引言
Nginx作為高性能的web和反向代理服務器,在互聯網公司應用廣泛。作為一名剛入職的小白,9月底的時候經歷了公司站點的HTTPS改造,雖然沒有親手配置nginx, 而且一開始看到Nginx配置還是很懵逼的 (為什么本科學校不專門學一下啊?%>_<% 還是怪自己不夠主動去學),本文寫給從沒接觸過Nginx的同學,也算是入門,不會太深入,有興趣的同學可以買《深入理解Nginx》, (真的要有興趣啊,很厚的一本書),個人覺得作為開發人員知道一些基本配置就行了,沒必要特別深入。
Nginx的安裝
首先介紹一下Nginx的在不同系統下的安裝
# CentOS
yum install nginx;
# Ubuntu
sudo apt-get install nginx;
# Mac
brew install nginx;
本文主要以Mac下的安裝為例。
通過homebrew,nginx默認被安裝在/usr/local/Cellar/nginx/
目錄下。conf安裝目錄在/usr/local/etc/nginx/nginx.conf
啟動、熱重啟、關閉以及測試配置的命令如下:
# 啟動
nginx -s start;
# 重新啟動,熱啟動,修改配置重啟不影響線上
nginx -s reload;
# 關閉
nginx -s stop;
# 修改配置后,可以通過下面的命令測試是否有語法錯誤
nginx -t;
在瀏覽器中鍵入http://localhost:8080,即可訪問到nginx的歡迎界面。那么,為什么會訪問到nginx的歡迎界面的呢?
不妨打開nginx.conf,一起來分析一下這個文件。在nginx的配置中用#
表示注釋
#user nobody;
##定義擁有和運行Nginx服務的Linux系統用戶
worker_processes 1;
##定義單進程。通常將其設成CPU的個數或者內核數
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
##定義Nginx在哪里打日志
#pid logs/nginx.pid;
##Nginx寫入主進程ID(PID)
events {
worker_connections 1024;
##通過worker_connections和worker_processes計算maxclients。
##max_clients = worker_processes * worker_connections
}
http {
include mime.types;
##在/opt/nginx/conf/mime.types寫的配置將在http模塊中解析
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;
##如果是為了獲取本地存儲的靜態化文件,sendfile可以加速服務端,但是如果是反向代理,那么該功能就失效了。
#tcp_nopush on;
##在 nginx 中,tcp_nopush 配置和 tcp_nodelay "互斥"。它可以配置一次發送數據的包大小。也就是說,它不是按時間累計 0.2 秒后發送包,而是當包累計到一定大小后就發送。在 nginx 中,tcp_nopush 必須和sendfile 搭配使用。
#keepalive_timeout 0;
keepalive_timeout 65;
##設置保持客戶端連接時間
#gzip on;
##告訴服務端用gzip壓縮
server {
##如果你想對虛擬主機進行配置,可以在單獨的文件中配置server模塊,然后include進來
listen 8080;
##告訴Nginx TCP端口,監聽HTTP連接。listen 80; 和 listen *:80;是一樣的
server_name localhost;
##定義虛擬主機的名字
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
##location模塊可以配置nginx如何反應資源請求
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;
# }
#}
include servers/*;
}
雖然上面的默認配置很多,但是可以總體歸納為三個模塊:
#全局模塊
events {
#events模塊
}
http
{
#http全局模塊
server
{
#server全局模塊
location [PATTERN]{
#location模塊
}
}
}
1、全局塊:配置影響nginx全局的指令。一般有運行nginx服務器的用戶組,nginx進程pid存放路徑,日志存放路徑,配置文件引入,允許生成worker process數等。
2、events塊:配置影響nginx服務器或與用戶的網絡連接。有每個進程的最大連接數,選取哪種事件驅動模型處理連接請求,是否允許同時接受多個網路連接,開啟多個網絡連接序列化等。
3、http塊:可以嵌套多個server,配置代理,緩存,日志定義等絕大多數功能和第三方模塊的配置。如文件引入,mime-type定義,日志自定義,是否使用sendfile傳輸文件,連接超時時間,單連接請求數等。
4、server塊:配置虛擬主機的相關參數,一個http中可以有多個server。
5、location塊:配置請求的路由,以及各種頁面的處理情況。
Nginx配置Web服務器
先介紹對一個web服務進行簡單配置,然后對各個重要點簡單說明。這個案例中關于反向代理的要點將在下一篇中介紹。
案列
########### 每個指令必須有分號結束。#################
#user administrator administrators; #配置用戶或者組,默認為nobody nobody。
#worker_processes 2; #允許生成的進程數,默認為1
#pid /nginx/pid/nginx.pid; #指定nginx進程運行文件存放地址
error_log log/error.log debug; #制定日志路徑,級別。這個設置可以放入全局塊,http塊,server塊,級別以此為:debug|info|notice|warn|error|crit|alert|emerg
events {
accept_mutex on; #設置網路連接序列化,防止驚群現象發生,默認為on
multi_accept on; #設置一個進程是否同時接受多個網絡連接,默認為off
#use epoll; #事件驅動模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport
worker_connections 1024; #最大連接數,默認為512
}
http {
include mime.types; #文件擴展名與文件類型映射表
default_type application/octet-stream; #默認文件類型,默認為text/plain
#access_log off; #取消服務日志
log_format myFormat '$remote_addr–$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for'; #自定義格式
access_log log/access.log myFormat; #combined為日志格式的默認值
sendfile on; #允許sendfile方式傳輸文件,默認為off,可以在http塊,server塊,location塊。
sendfile_max_chunk 100k; #每個進程每次調用傳輸數量不能大于設定的值,默認為0,即不設上限。
keepalive_timeout 65; #連接超時時間,默認為75s,可以在http,server,location塊。
upstream mysvr {
server 127.0.0.1:7878;
server 192.168.10.121:3333 backup; #熱備
}
error_page 404 https://www.baidu.com; #錯誤頁
server {
keepalive_requests 120; #單連接請求上限次數。
listen 4545; #監聽端口
server_name 127.0.0.1; #監聽地址
location ~*^.+$ { #請求的url過濾,正則匹配,~為區分大小寫,~*為不區分大小寫。
#root path; #根目錄
#index vv.txt; #設置默認頁
proxy_pass http://mysvr; #請求轉向mysvr 定義的服務器列表
deny 127.0.0.1; #拒絕的ip
allow 172.18.5.54; #允許的ip
}
}
}
域名與端口配置
上述例子中 listen 4545; #監聽端口
表示監聽端口是4545。但是對于一個小白來說有時候看到 listen [::]:80;
,listen :80;
,listen *:80;
這三種寫法還是會很懵逼的,那么他們之間有什么區別啊?
listen [::]:80;
表示Nginx會同時監聽IPv4和IPv6的80端口,listen :80;
,listen *:80;
這兩種寫法是一樣的,
location中URL匹配
上述例子中,大家發現location 后面跟著的正則匹配,其實在nginx中,location url 匹配是遵循一定優先級的。
location = / {
# 完全匹配 =
# 大小寫敏感 ~
# 忽略大小寫 ~*
}
location ^~ /images/ {
# 前半部分匹配 ^~
# 匹配任何以 /images/ 開頭的地址,匹配符合以后,停止往下搜索正則,采用這一條。
}
location ~* \.(gif|jpg|jpeg)$ {
# ~* 表示執行一個正則匹配,不區分大小寫
# ~ 表示執行一個正則匹配,區分大小寫
# 匹配所有以 gif,jpg或jpeg 結尾的請求
}
location / {
# 如果以上都未匹配,會進入這里
}
location中的優先級如下
(location =) > (location 完整路徑) > (location ^~ 路徑) > (location ,* 正則順序) > (location 部分起始路徑) > (/)
location = / {
#僅僅匹配請求
[ configuration A ]
}
location / {
#匹配所有以 / 開頭的請求。但是如果有更長的同類型的表達式,則選擇更長的表達式。如果有正則表達式可以匹配,則優先匹配正則表達式。
[ configuration B ]
}
location /documents/ {
# 匹配所有以 /documents/ 開頭的請求。但是如果有更長的同類型的表達式,則選擇更長的表達式。
#如果有正則表達式可以匹配,則優先匹配正則表達式。
[ configuration C ]
}
location ^~ /images/ {
# 匹配所有以 /images/ 開頭的表達式,如果匹配成功,則停止匹配查找。所以,即便有符合的正則表達式location,也
# 不會被使用
[ configuration D ]
}
location ~* \.(gif|jpg|jpeg)$ {
# 匹配所有以 gif jpg jpeg結尾的請求。但是 以 /images/開頭的請求,將使用 Configuration D
[ configuration E ]
}
文件路徑定義
在location模塊中可以定義文件路徑
比如
根目錄設置:
location / {
root /home/barret/test/;
}
主頁設置:
index /html/index.html /php/index.php;
try_files 設置
try_file主要是功能是去檢查文件是否存在,使用第一個被找到文件返回。如果沒有一個文件找到, 那么重定向到最后一個參數指定的URI。如:
location /images/ {
try_files $uri /images/default.gif;
}
location = /images/default.gif {
expires 30s;
}
ps: $uri 是不帶請求參數的當前URI,下面的全局變量中會介紹
,最后一個參數也可以是命名的location。如下:
try_files $uri $uri.html $uri/index.html @other;
location @other {
# 嘗試尋找匹配 uri 的文件,失敗了就會轉到上游處理
proxy_pass http://localhost:9000;
}
location / {
# 嘗試尋找匹配 uri 的文件,沒找到直接返回 502
try_files $uri $uri.html =502;
}
Rewrite 重定向
如果要把一個URL http://www.lxweimin.com/users/10001 重寫成 http://www.lxweimin.com/show?user=10001,可以使用rewrite 規則,參見下面的代碼。我在公司站點的改造過程中,遇到了rewrite,重寫URL目的是為了更好的SEO。
location /users/ {
rewrite ^/users/(.*)$ /show?user=$1 break;
}
rewrite功能就是,使用nginx提供的全局變量或自己設置的變量,結合正則表達式和標志位實現url重寫以及重定向。
rewrite 規則 定向路徑 重寫類型;
1、規則:可以是字符串或者正則來表示想匹配的目標url
2、定向路徑:表示匹配到規則后要定向的路徑,如果規則里有正則,則可以使用$index來表示正則里的捕獲分組
3、重寫類型:
last :相當于Apache里德(L)標記,表示完成rewrite,瀏覽器地址欄URL地址不變
break;本條規則匹配完成后,終止匹配,不再匹配后面的規則,瀏覽器地址欄URL地址不變
redirect:返回302臨時重定向,瀏覽器地址會顯示跳轉后的URL地址
permanent:返回301永久重定向,瀏覽器地址欄會顯示跳轉后的URL地址
break 與 last的區別
- last一般寫在server和if中,而break一般使用在location中
- last不終止重寫后的url匹配,即新的url會再從server走一遍匹配流程,而break終止重寫后的匹配
- break和last都能組織繼續執行后面的rewrite指令
在location里一旦返回break則直接生效并停止后續的匹配location
舉個例子:
server {
location / {
rewrite /last/ /q.html last;
rewrite /break/ /q.html break;
}
location = /q.html {
return 400;
}
}
- 訪問/last/時重寫到/q.html,然后使用新的uri再匹配,正好匹配到locatoin = /q.html然后返回了400
- 訪問/break時重寫到/q.html,由于返回了break,則直接停止了
if表達式
上面的簡單重寫很多時候滿足不了需求,比如需要判斷當文件不存在時、當路徑包含xx時等條件,則需要用到if
if的語法如下:
if (表達式) {
}
內置的全局變量:
$args :這個變量等于請求行中的參數,同$query_string
$content_length : 請求頭中的Content-length字段。
$content_type : 請求頭中的Content-Type字段。
$document_root : 當前請求在root指令中指定的值。
$host : 請求主機頭字段,否則為服務器名稱。
$http_user_agent : 客戶端agent信息
$http_cookie : 客戶端cookie信息
$limit_rate : 這個變量可以限制連接速率。
$request_method : 客戶端請求的動作,通常為GET或POST。
$remote_addr : 客戶端的IP地址。
$remote_port : 客戶端的端口。
$remote_user : 已經經過Auth Basic Module驗證的用戶名。
$request_filename : 當前請求的文件路徑,由root或alias指令與URI請求生成。
$scheme : HTTP方法(如http,https)。
$server_protocol : 請求使用的協議,通常是HTTP/1.0或HTTP/1.1。
$server_addr : 服務器地址,在完成一次系統調用后可以確定這個值。
$server_name : 服務器名稱。
$server_port : 請求到達服務器的端口號。
$request_uri : 包含請求參數的原始URI,不包含主機名,如:”/foo/bar.php?arg=baz”。
$uri : 不帶請求參數的當前URI,$uri不包含主機名,如”/foo/bar.html”。
$document_uri : 與$uri相同。
內置的條件判斷:
-f和!-f用來判斷是否存在文件
-d和!-d用來判斷是否存在目錄
-e和!-e用來判斷是否存在文件或目錄
-x和!-x用來判斷文件是否可執行
有時候在配置文件中看到$http_host
。他和$host
有什么不同呢?
$http_host
和$host
都是原始的’HOST’字段
比如請求的時候HOST的值是www.csdn.net
那么反代后還是www.csdn.net
如果客戶端發過來的請求的header中沒有有’HOST’這個字段時,
建議使用$host
,這時候的$host
就等于server_name
。
if 表達式例子:
# 如果文件不存在則返回400
if (!-f $request_filename) {
return 400;
}
# 如果host不是xuexb.com,則301到xuexb.com中
if ( $host != 'xuexb.com' ){
rewrite ^/(.*)$ https://xuexb.com/$1 permanent;
}
# 如果請求類型不是POST則返回405
if ($request_method = POST) {
return 405;
}
# 如果參數中有 a=1 則301到指定域名
if ($args ~ a=1) {
rewrite ^ http://example.com/ permanent;
}
if 通常與location規則搭配使用,如:
# 訪問 /test.html 時
location = /test.html {
# 默認值為xiaowu
set $name xiaowu;
# 如果參數中有 name=xx 則使用該值
if ($args ~* name=(\w+?)(&|$)) {
set $name $1;
}
# 301
rewrite ^ /$name.html permanent;
}
上面表示:
- /test.html => /xiaowu.html
- /test.html?name=ok => /ok.html?name=ok
本文主要參考以下這個文獻
http://www.linuxeye.com/configuration/2657.html
感謝閱讀,下一篇文章會介紹Nginx 配置反向代理服務器
下面是些面試資料
https://zfau4tzxp5.feishu.cn/docx/ByM0dxtWoo87QtxnWtTcG0kCnpg