全局配置段
# 允許運行nginx服務器的用戶和用戶組
user www-data;
# 併發連接數處理(進程數量),跟cpu核數保存一致;
worker_processes auto;
# 存放 nginx的進程號
pid /run/nginx.pid;
# 定義相關屬性
events {
# 一個進程允許處理的最大連接數768
worker_connections 768;
# multi_accept on;
# 定義使用的內核模型,默認開啓epoll
# use epoll
}
http配置段
http {
# 開啓高效文件傳輸模式
sendfile on;
###### 防止網絡阻塞 ######
tcp_nopush on;
tcp_nodelay on;
# 長連接超時時間65秒
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
###### 允許處理文件類型 ######
# 文件擴展名與文件類型映射表
include /etc/nginx/mime.types;
# 默認文件類型
default_type application/octet-stream;
###### 日誌配置 ######
# Logging Settings
##
# 日誌文件存放
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
# 開啓gzip壓縮輸出
gzip on;
# 開啓目錄列表訪問,搭建下載服務時使用,默認關閉
# autoindex on;
# 配置文件從上往下讀,類似django路由轉發
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
# 如果不導入Nginx.conf配置,可以直接在這裏添加server
# server {
# listen 172.17.1.142:80;
# location /performance {
# alias /etc/nginx/my_gitbook/_book/;
# index index.html;
# }
# }
}
Server常見配置
server {
# 監聽端口號
listen ip:port;
server_name 主機名;
...
}
listen的表示方式
形式 | 描述 |
---|---|
ip:port | 精確表示 |
ip | 自動監聽80端口 |
port | 自動監聽80端口 |
default_server | 自動使用默認地址localhost:80 |
server_name
作用:對不同的域名的請求分類
格式:
形式 | 例子 |
---|---|
完整樣式 | www.example.com |
前綴正則樣式 | *.example.com |
後綴正則樣式 | www.example.* |
location
location 根據Server匹配到的請求路徑和關鍵字去響應和處理, 支持正則匹配
普通匹配:
相同的路由規則下:
類型 | 含義 | 匹配方式 | 優先級 | 樣式 |
---|---|---|---|---|
=/路徑 | 精確匹配 | 前綴 | 1 | location = /image {} |
^~ | 優先匹配 | 前綴 | 2 | location ^~ /page {} |
@ | 內部重定向 | 前綴 | location @name {} | |
空 / | 通用匹配 | 前綴 | location / {} |
# ----------匹配規則優先級---------------
# 同樣對於根目錄匹配
# 通用匹配 最後匹配
location / {
return 400;
}
精確匹配 優先級最高
location = / {
return 401;
}
優先匹配 優先級第二
location ~ / {
return 402;
}
正則匹配:
略
location 基礎設置
location / {
root /var/www/html; # 指定響應請求的文件所在路徑
index index.php index.html index.htm; # 指定響應請求的默認文件名稱
expires 7d; # 指定響應請求的文件過期時間,一般用於靜態文件
try_files $uri $uri/ =404; # 如果root指定的路徑下有查找的文件,就返回,否則報錯
}
- root屬性
作用:定義Server相應請求的html文件所在路徑
表現形式: root /var/www/html;
- index屬性
作用:定義響應請求後返回的文件名稱或格式
表現形式: index index.html index.htm index.nginx-debian.html;
- return屬性
作用:定義響應請求後返回的http狀態碼
表現形式: return 400;
location 臨時跳轉
location = /test/ {
return 302 http://sswang.com/; # 訪問舊url的時候,臨時跳轉到新url,兩個url均不失效
}
location 訪問控制
location /nginx-status {
stub_status on; # 開啓nginx的狀態頁面,返回nginx狀態信息,默認關閉
allow 192.168.1.11; # 指定ip客戶端允許的訪問(ip白名單)
deny all; # 除了允許都拒絕
}
location 目錄列表
# 目錄下載控制
location /upload {
alias /etc/nginx; # 指定查看文件列表路徑(絕對路徑)
autoindex on; # 開啓目錄自動索引
autoindex_exact_size off; # 默認on,顯示文件確切大小(bytes)。off表示顯示文件的大概大小(kB/MB/...)
autoindex_localtime on; # 默認off,顯示的文件時間爲GMT時間。on表示顯示文件的服務器時間
}
注意:
該alias指定的目錄下,不允許出現index屬性指定的文件。
root 和 alias 區別
一般情況下,在location /中配置root,在location /other中配置alias
root 表示 location 匹配內容的相對路徑
alias 表示 一個絕對路徑,而且必須以"/"結尾
- root
訪問http://localhost/img/,nginx找/var/www/image/img/目錄下的文件
location /img/ {
root /var/www/image/;
}
- alias
訪問http://localhost/img/,nginx找/var/www/image/目錄下的文件
location /img/ {
alias /var/www/image/;
}
try_files 和return的使用和區別
都是結果狀態返回,但他們有 不同的使用場景,try_files對根路徑 /匹配無效
- try_files
location /a {
try_files $uri $uri/ =400;
}
- try_files和@使用
# try_files配合@使用
location /a {
try_files $uri $uri/ @test;
}
location @test {
return https://www.baidu.com;
}
try_files配合@使用
- return
location / {
return 302 https://www.baidu.com;
}