
用alias 指定資源路徑時,我們通路/test/index.html,它傳回/data/web/html/index.html,相當于alias 指定的資源路徑覆寫了使用者請求的URI最右側的“/”,換句話說使用者URI最右側的“/”就是alias所指定的資源路徑,使用者通路/test/index.html 就相當于通路/data/web/html/index.html;這裡還需要注意一點的是 alias 指定資源路徑時,必須是“/”結尾,如果不以“/”結尾,資源将無法找到;對于root來講是不是“/”結尾這個無要求;
上一篇部落格我們大概介紹了一下nginx,nginx的架構,nginx編譯安裝和nginx指令的用法,回顧請參考https://www.cnblogs.com/qiuhom-1874/p/12366808.html;今天我們來簡單的配置下nginx和一些簡單指令說明。
nginx和httpd類似都是高度子產品化的軟體,不同的子產品有着不同的功能,想要把nginx配置好,首先我們需要了解各個子產品的用法以及子產品選項的用法和說明。首先我們來了解下nginx用yum安裝後的程式環境。
[root@www ~]# rpm -ql nginx
/etc/logrotate.d/nginx
/etc/nginx/fastcgi.conf
/etc/nginx/fastcgi.conf.default
/etc/nginx/fastcgi_params
/etc/nginx/fastcgi_params.default
/etc/nginx/koi-utf
/etc/nginx/koi-win
/etc/nginx/mime.types
/etc/nginx/mime.types.default
/etc/nginx/nginx.conf
/etc/nginx/nginx.conf.default
/etc/nginx/scgi_params
/etc/nginx/scgi_params.default
/etc/nginx/uwsgi_params
/etc/nginx/uwsgi_params.default
/etc/nginx/win-utf
/usr/bin/nginx-upgrade
/usr/lib/systemd/system/nginx.service
/usr/lib64/nginx/modules
/usr/sbin/nginx
/usr/share/doc/nginx-1.16.1
……省略部分内容
/var/lib/nginx
/var/lib/nginx/tmp
/var/log/nginx
[root@www ~]#
提示:從上面的顯示,我們大概可以了解到nginx的主配置檔案是/etc/ngxin/ngxin.conf,nginx.conf.default是預設配置檔案,從這個檔案中我們可以了解到nginx的預設配置是怎麼配置的;主程式是/usr/sbin/nginx,日志檔案路徑是/var/log/nginx,Unit File是nginx.service;/etc/nginx/fastcgi.conf和fastcgi_parems,這兩個檔案一個是fastcig協定的配置檔案,一個是變量配置檔案。了解了nginx 的程式環境,我們在來看看主配置檔案内容
[root@www ~]# cat /etc/nginx/nginx.conf
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
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 /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2 default_server;
# listen [::]:443 ssl http2 default_server;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# location / {
# }
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
}
[root@www ~]#
提示:主配置檔案結構大緻可以分為main段(全局配置段)和http配置段或者mail配置段或者stream段,後面的http配置段或mail配置段或stream配置段,主要看nginx用于什麼功能,如果單純的用于web伺服器,那麼後面的mail和stream配置段就可以不要了,也就是說有關web的配置我們必須要在http配置段配置;同樣的如果nginx用于郵件代理我們就需要把有關郵件代理的配置放到mail配置段,如果用于四層負載均衡,我們需要把對應的配置寫到stream配置段;我們先說一下全局配置段吧
user指令:表示指定運作worker程序的使用者
[root@www ~]# head /etc/nginx/nginx.conf
For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
[root@www ~]# ps aux |grep nginx
root 1425 0.0 0.0 120832 2244 ? Ss 19:49 0:00 nginx: master process nginx
nginx 1426 0.0 0.0 121228 3132 ? S 19:49 0:00 nginx: worker process
nginx 1427 0.0 0.0 121228 3132 ? S 19:49 0:00 nginx: worker process
nginx 1428 0.0 0.0 121228 3132 ? S 19:49 0:00 nginx: worker process
nginx 1429 0.0 0.0 121228 3132 ? S 19:49 0:00 nginx: worker process
root 1439 0.0 0.0 112660 968 pts/0 S+ 19:51 0:00 grep --color=auto nginx
[root@www ~]#
提示:通常情況都不建議nginx用root運作;如果是叢集環境建議統一程序運作使用者,其次必須統一時間
worker_processes :指定worker程序的數量,一般是和運作nginx主機的CUP核心數來定,一般都是小于或者等于實體cpu核心數,auto表示自動去比對cup核心數來啟動worker程序數量
[root@www ~]# lscpu
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 4
On-line CPU(s) list: 0-3
Thread(s) per core: 1
Core(s) per socket: 2
Socket(s): 2
NUMA node(s): 1
Vendor ID: GenuineIntel
CPU family: 6
Model: 158
Model name: Intel(R) Core(TM) i7-7700 CPU @ 3.60GHz
Stepping: 9
CPU MHz: 3599.644
CPU max MHz: 0.0000
CPU min MHz: 0.0000
BogoMIPS: 7200.06
Hypervisor vendor: VMware
Virtualization type: full
L1d cache: 32K
L1i cache: 32K
L2 cache: 256K
L3 cache: 8192K
NUMA node0 CPU(s): 0-3
Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts nopl xtopology tsc_reliable nonstop_tsc aperfmperf eagerfpu pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 invpcid rtm rdseed adx smap xsaveopt xsavec xgetbv1 dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp
[root@www ~]# ps aux |grep nginx
root 1425 0.0 0.1 121500 5272 ? Ss 19:49 0:00 nginx: master process nginx
nginx 1453 0.0 0.0 121748 3668 ? S 19:56 0:00 nginx: worker process
nginx 1454 0.0 0.0 121748 3668 ? S 19:56 0:00 nginx: worker process
nginx 1455 0.0 0.0 121748 3668 ? S 19:56 0:00 nginx: worker process
nginx 1456 0.0 0.0 121748 3668 ? S 19:56 0:00 nginx: worker process
root 1465 0.0 0.0 112660 972 pts/0 S+ 19:57 0:00 grep --color=auto nginx
[root@www ~]#
error_log表示指定nginx錯誤日志存放檔案
[root@www ~]# ll /var/log/nginx/error.log
-rw-r--r-- 1 root root 120 Feb 27 19:56 /var/log/nginx/error.log
[root@www ~]# cat /var/log/nginx/error.log
2020/02/27 19:52:18 [notice] 1442#0: signal process started
2020/02/27 19:56:47 [notice] 1452#0: signal process started
[root@www ~]#
pid表示指定pid檔案
[root@www ~]# ps aux |grep nginx
root 1567 0.0 0.0 120832 2248 ? Ss 20:05 0:00 nginx: master process /usr/sbin/nginx
nginx 1568 0.0 0.0 121228 3336 ? S 20:05 0:00 nginx: worker process
nginx 1569 0.0 0.0 121228 3336 ? S 20:05 0:00 nginx: worker process
nginx 1570 0.0 0.0 121228 3336 ? S 20:05 0:00 nginx: worker process
nginx 1571 0.0 0.0 121228 3136 ? S 20:05 0:00 nginx: worker process
root 1574 0.0 0.0 112660 972 pts/0 S+ 20:05 0:00 grep --color=auto nginx
[root@www ~]# ll /var/run/nginx.pid
-rw-r--r-- 1 root root 5 Feb 27 20:05 /var/run/nginx.pid
[root@www ~]# nginx -s stop
[root@www ~]# ll /var/run/nginx.pid
ls: cannot access /var/run/nginx.pid: No such file or directory
[root@www ~]#
提示:pid檔案就是存放nginx主要程序的程序号的,如果nginx沒有運作或者停止了服務,那麼pid檔案也會跟着消失;這裡提示一下在centos7上/var/run 和/run是同一檔案夾 ,它倆做的是硬連結
[root@www ~]# ll -id /var/run/
1150 drwxr-xr-x 22 root root 620 Feb 27 20:07 /var/run/
[root@www ~]# ll -id /run
1150 drwxr-xr-x 22 root root 620 Feb 27 20:07 /run
[root@www ~]#
提示:兩個檔案夾的inode号都是同一個
include此指令表示把某些地方的配置導入到此地;這個指定配置的時候需要注意放的位置;正因為有了這個功能,我們就可以把很多不同功能的配置用專有的檔案來配置,這樣既友善管理,也很容易讀;
events此配置段表示配置有關事件驅動相關的配置
worker_connections :每個worker程序所能夠打開的最大并發連接配接數;
use method:指定并發請求的處理方法;如use epoll;
accept_mutex on|off:處理新的連接配接請求的方法;on表示各worker程序輪流處理新請求,off表示每來一個新請求就會通知所有的worker程序
有關性能優化的全局配置
worker_cpu_affinity cpumask:手動或自動綁定cpu,預設情況下是沒有綁定cpu的,這意味着worker程序會在每個CPU上來會排程的,這樣一來在cpu就存在頻繁的切換,影響性能;我們可以手動把每個程序綁定到不同的CPU上。禁止worker程序在每個CPU上來回切換
提示:在沒有綁定cpu時,我們對nginx worker程序發起并發連接配接請求,可以看到4個worker程序在不同的CUP上來回切換,很顯然這無疑在給系統多餘的開銷,我們可以綁定nginx 的worker線程。
[root@www ~]# grep worker_cpu /etc/nginx/nginx.conf
worker_cpu_affinity 0001 0010 0100 1000;
[root@www ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@www ~]# nginx -s reload
[root@www ~]#
提示:如果你有的CPU是八核的,那麼就需要用8個0來表示,其中第一個程序對應最右側的0,如果需要綁定到該cpu核心上,則對應位為1即可;
提示:綁定cpu我們也可以直接使用worker_cpu_affinity auto;來指定,讓其自動綁定到每個cpu核心上去
worker_priority number:指定worker程序的nice值,設定worker程序優先級;[-20,19]
[root@www ~]# grep "worker_priority" /etc/nginx/nginx.conf
worker_priority -5;
[root@www ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@www ~]# nginx -s reload
[root@www ~]# ps axo comm,pid,nice,psr|grep nginx
nginx 2583 0 0
nginx 31567 -5 0
nginx 31568 -5 1
nginx 31569 -5 2
nginx 31570 -5 3
[root@www ~]#
以上就是常用的全局配置段指令的說明和使用,詳細請參考nginx官方文檔http://nginx.org/en/docs/ngx_core_module.html
http協定的相關配置
在主配置檔案中我們可以看到有一個以http開頭的配置段,這個配置段主要配置nginx工作成web服務的配置
server:這個指令表示定義個虛拟主機類似httpd裡的virtualhost,這也是一個http裡的一個子配置段,裡面有server_name指令 root等等
server_name:表示指定虛拟主機名稱;指明虛拟主機的主機名稱;後可跟多個由空白字元分隔的字元串;支援*通配任意長度的任意字元;支援~起始的字元做正規表達式模式比對;
比對機制:首先是字元串精确比對;其次是左側*通配符,然後右側*通配符,最後是正規表達式
root:設定web資源路徑映射;用于指明使用者請求的url所對應的本地檔案系統上的文檔所在目錄路徑;可用的位置:http, server, location, if in location;
listen:指定虛拟主機監聽的位址和端口,如果隻指定端口未指定位址,表示監聽伺服器上的所有位址,如果在server裡沒有指定端口,對應的虛拟主機将監聽在預設端口80上
listen address[:port] [default_server] [ssl] [http2 | spdy] [backlog=number] [rcvbuf=size] [sndbuf=size]
default_server:設定為預設虛拟主機;
ssl:限制僅能通過ssl連接配接該服務
backlog=number:指定後援隊列長度
sndbuf=size:指定發送緩沖區大小
提示:我們這樣配置後,在hosts檔案中添加對應的解析後,用浏覽器通路www.ilinux.io,此時它就會把預設主機裡的映射路徑下的資源響應我們
[root@www ~]# echo "this is default path " > /usr/share/nginx/html/test.html
[root@www ~]# cat /usr/share/nginx/html/test.html
this is default path
[root@www ~]# curl http://www.ilinux.io/test.html
this is default path
[root@www ~]#
tcp_nodelay on|off :在keepalived模式下的連接配接是否啟用TCP_NODELAY選項;
tcp_nopush on|off:在sendfile模式下,是否啟用TCP_CORK選項;
sendfile on|off:是否啟用sendfile功能;
通常情況下以上三項都是打開的,TCP_NODELAY主要是發送封包延時問題,如果開啟了該功能選項,表示不管資料包多小都及時發送,如果關閉了,通常會等到一定量的資料封包一起發送,對于小封包延時就很高,TCP_CORK主要是解決小包問題,它和TCP_NODELAY相反,啟用表示要到一定量的資料包後才發送,關閉表示不用等一定量的資料封包再發送,它們兩者都是解決小包問題,前者應用在長連接配接模式下,後者應用在sendfile模式下;sendfile模式解決了核心到使用者應用程式,使用者應用程式到核心的重複過程,它可将資料封包直接從核心加載到網卡socket緩存區,直接發送出去;這三項都和性能相關,通常都是開啟的;
location:此指令用于實作從uri到檔案系統的路徑映射;ngnix會根據使用者請求的URI來檢查定義的所有location,并找出一個最佳比對,而後應用其配置;在一個server中location配置段可存在多個;
文法:location [ = | ~ | ~* | ^~ ] uri { ... }
=:對URI做精确比對;
^~:對URI的左半部分做比對檢查,不區分字元大小寫;
~:對URI做正規表達式模式比對,區分字元大小寫;
~*:對URI做正規表達式模式比對,不區分字元大小寫;
不帶符号:比對起始于此uri的所有的url;
比對優先級:=, ^~, ~/~*,不帶符号;
示例:
location = / {
[ configuration A ]
}
location / {
[ configuration B ]
}
location /documents/ {
[ configuration C ]
}
location ^~ /images/ {
[ configuration D ]
}
location ~* \.(gif|jpg|jpeg)$ {
[ configuration E ]
}
說明:如果是使用者請求uri是/ 那麼在以上location中将比對到A,如果是/index 将比對到B,如果是/documents/index将比對到C,如果是/images/1.jpg将比對到D和E,但是D的優先級高于E,所有應用D的配置,如果是/document/1.jpg将比對到C和E,但是E的優先級高于C,是以會應用E的配置;
alias path:定義資源路徑别名,僅用于location中;它和root定義資源路徑不同的是,root定義的資源路徑應用在/uri/左側的'/',而alias定義的資源路徑應用在/uri/的右側'/';
示例:
[root@www ~]# cat /etc/nginx/conf.d/test.conf
server {
listen 80;
server_name www.ilinux.io;
location /test/ {
root /data/web/html/;
allow all;
}
}
[root@www ~]# cat /data/web/html/index.html
this is /data/web/html/index.html
[root@www ~]# cat /data/web/html/index.html
this is /data/web/html/index.html
[root@www ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@www ~]# nginx -s reload
[root@www ~]# curl http://www.ilinux.io/test/index.html
this is /data/web/html/test/index.html
[root@www ~]#
提示:我們用root來指定資源路徑時,我們通路/test/.index.html 它傳回的是/data/web/html/test/index.html,就相當于把location左側的“/”更換成root定義的路徑,使用者通路資源的真實路徑就是/data/web/html/test/index.html;換句話講,root指定資源路徑,比對使用者URI最左側“/”,真實路徑是root指定的路徑+使用者URI(不帶左側"/")
[root@www ~]# cat /etc/nginx/conf.d/test.conf
server {
listen 80;
server_name www.ilinux.io;
location /test/ {
alias /data/web/html/;
allow all;
}
}
[root@www ~]# cat /data/web/html/index.html
this is /data/web/html/index.html
[root@www ~]# cat /data/web/html/test/index.html
this is /data/web/html/test/index.html
[root@www ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@www ~]# nginx -s reload
[root@www ~]# curl http://www.ilinux.io/test/index.html
this is /data/web/html/index.html
[root@www ~]#
提示:用alias 指定資源路徑時,我們通路/test/index.html,它傳回/data/web/html/index.html,相當于alias 指定的資源路徑覆寫了使用者請求的URI最右側的“/”,換句話說使用者URI最右側的“/”就是alias所指定的資源路徑,使用者通路/test/index.html 就相當于通路/data/web/html/index.html;這裡還需要注意一點的是 alias 指定資源路徑時,必須是“/”結尾,如果不以“/”結尾,資源将無法找到;對于root來講是不是“/”結尾這個無要求;
index file:指定預設首頁,可配置在http, server, location;
[root@www html]# cat /etc/nginx/conf.d/test.conf
server {
listen 80;
server_name www.ilinux.io;
location /test/ {
alias /data/web/html/;
index test.html;
allow all;
}
}
[root@www html]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@www html]# nginx -s reload
[root@www html]# curl http://www.ilinux.io/test/
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.16.1</center>
</body>
</html>
[root@www html]# echo "this is default page" > /data/web/html/test.html
[root@www html]# curl http://www.ilinux.io/test/
this is default page
[root@www html]#
error_page code ... [=[response]] uri:指定錯誤頁面,比對指定的狀态碼,傳回指定的URL
[root@www html]# cat /etc/nginx/conf.d/test.conf
server {
listen 80;
server_name www.ilinux.io;
location /test/ {
alias /data/web/html/;
index test.html;
allow all;
}
error_page 404 403 /error.html;
location /error.html {
root /data/web/html/error;
}
}
[root@www html]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@www html]# nginx -s reload
[root@www html]# mkdir /data/web/html/error/
[root@www html]# echo "error page" > /data/web/html/error/error.html
[root@www html]# curl http://www.ilinux.io/abc/
error page
[root@www html]#
提示:通過指定錯誤頁面,我們可以自定義錯誤頁面,也可以對錯誤頁面用專有的location來做配置;
作者:Linux-1874
出處:https://www.cnblogs.com/qiuhom-1874/
本文版權歸作者和部落格園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接配接,否則保留追究法律責任的權利.