Ngnix已經廣泛應用於J-one和Jdos的環境部署上,本文對Ngnix的常用的配置和基本功能進行講解,適合Ngnix入門學習。
找到Nginx安裝目錄下的conf目錄下nginx.conf文件,Ngnix的基本功能配置是由它提供的。
1.1 配置文件結構
Nginx的配置文件(conf/nginx.conf)整體上分爲如下幾個部分:
區域 |
職業 |
全局塊 | 配置和Nginx運行相關的全局配置 |
events塊 | 配置和網絡鏈接相關的配置 |
http塊 | 配置代理、緩存、日誌記錄、虛擬主機等配置 |
server塊 | 配置虛擬主機的相關參數,一個http塊中可以有多個server塊 |
location塊 | 配置請求的路由,以及各種頁面的處理情況 |
配置層級圖如下所示。
# 以下是全局段配置
#user administrator administrators; #配置用戶或者組,默認爲nobody nobody。
#worker_processes 2; #設置進程數,默認爲1
#pid /nginx/pid/nginx.pid; #指定nginx進程運行文件存放地址
error_log log/error.log debug; #制定日誌路徑,級別:debug|info|notice|warn|error|crit|alert|emerg
# events段配置信息
events {
accept_mutex on; #設置網路連接序列化,防止驚羣現象發生,默認爲on
multi_accept on; #設置一個進程是否同時接受多個網絡連接,默認爲off
#use epoll; #事件驅動模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport
worker_connections 1024; #最大連接數,默認爲512
}
# http、配置請求信息
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區塊開始,表示一個獨立的虛擬主機站點
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
}
}
}
-
[不加] < [~/~*] < [^~] < [=] -
示例如下:
location = / {
# 精確匹配/,主機名後面不能帶任何字符串 /
# 只匹配http://abc.com
# http://abc.com [匹配成功]
# http://abc.com/index [匹配失敗]
}
location ^~ /img/ {
#以 /img/ 開頭的請求,都會匹配上
#http://abc.com/img/a.jpg [成功]
#http://abc.com/img/b.mp4 [成功]
}
location ~* /Example/ {
# 則會忽略 uri 部分的大小寫
#http://abc.com/test/Example/ [匹配成功]
#http://abc.com/example/ [匹配成功]
}
location /documents {
# 如果有正則表達式可以匹配,則優先匹配正則表達式。
#http://abc.com/documentsabc [匹配成功]
}
location / {
#http://abc.com/abc [匹配成功]
}
2.1 反向代理概念:
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://localhost:8081;
proxy_set_header Host $host:$server_port;#爲請求頭添加Host字段,用於指定請求服務器的域名/IP地址和端口號。
# 設置用戶ip地址
proxy_set_header X-Forwarded-For $remote_addr;#爲請求頭添加XFF字段,值爲客戶端的IP地址。
# 當請求服務器出錯去尋找其他服務器
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503;
}
upstream web_servers {
server localhost:8081;
server localhost:8082;
}
server {
listen 80;
server_name localhost;
#access_log logs/host.access.log main;
location / {
proxy_pass http://web_servers;
proxy_set_header Host $host:$server_port;
}
}
upstream web_servers {
server 127.0.0.1:7878;
server 192.168.10.121:3333 backup; #熱備
}
upstream web_servers {
server localhost:8081 weight=1;
server localhost:8082 weight=2;
}
upstream test {
ip_hash;
server localhost:8080;
server localhost:8081;
}
upstream backend {
fair;
server localhost:8080;
server localhost:8081;
}
upstream backend {
hash_method crc32;
hash $request_uri;
server localhost:8080;
server localhost:8081;
}
upstream web_servers {
server localhost:8081;
server localhost:8082;
}
server {
listen 80;
server_name localhost;
set $doc_root /usr/local/var/www;
location ~* \.(gif|jpg|jpeg|png|bmp|ico|swf|css|js)$ {
root $doc_root/img;
}
location / {
proxy_pass http://web_servers;
proxy_set_header Host $host:$server_port;
}
error_page 500 502 503 504 /50x.html; #出現 500 502 503 504錯誤時走內部跳轉
location = /50x.html {
root $doc_root;
}
}
return code [text];
return code URL;
return URL;
例如:
location / {
return 404; # 直接返回狀態碼
}
location / {
return 404 "pages not found"; # 返回狀態碼 + 一段文本
}
location / {
return 302 /bbs ; # 返回狀態碼 + 重定向地址
}
location / {
return https://www.baidu.com ; # 返回重定向地址
}
-
last 重寫後的 URL 發起新請求,再次進入 server 段,重試 location 的中的匹配;
-
break 直接使用重寫後的 URL ,不再匹配其它 location 中語句;
-
redirect 返回302臨時重定向;
-
permanent 返回301永久重定向;
location /users/ {
rewrite ^/users/(.*)$ /show?user=$1 break;
}
server{
error_page 500 502 503 504 /50x.html;
location =/50x.html{
root html;
}
}
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 /usr/local/etc/nginx/logs/host.access.log main;
gzip on
#禁止訪問某個目錄
location / {
allow 192.168.0.0;
allow 127.0.0.1;
deny all;
#這段配置值允許192.168.0./24網段和127.0.0.1的請求,其他來源IP全部拒絕。
}
5.6 內置變量
-
$args :#這個變量等於請求行中的參數,同$query_string
-
$content_length :請求頭中的Content-length字段。
-
$content_type :請求頭中的Content-Type字段。
-
$document_root :當前請求在root指令中指定的值。
-
$host :請求行的主機名,爲空則爲請求頭字段 Host 中的主機名,再爲空則與請求匹配的server_name
-
$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相同
本文分享自微信公衆號 - 京東雲開發者(JDT_Developers)。
如有侵權,請聯繫 [email protected] 刪除。
本文參與“OSC源創計劃”,歡迎正在閱讀的你也加入,一起分享。