1、nginx常用指令
首先你需要進入nginx安裝目錄,裡面有一個可執nginx腳本檔案,如果你是按照我之前方式安裝nginx預設就會在/usr/sbin目錄下,如果你是自己編譯安裝的需要把nginx可執行檔案配置成環境變量或者進去安裝目錄執行指令。
1.啟動nginx
nginx
2.關閉nginx
nginx -s stop
3.檢查配置檔案
nginx -t
4.重新加載配置
nginx -s reload
2、nginx配置檔案
在Linux安裝nginx預設配置檔案都存放在/etc/nginx目錄,我們一般隻需要了解nginx.conf配置即可。
nginx 檔案結構
... #全局塊
events { #events塊
...
}
http #http塊
{
... #http全局塊
server #server塊
{
... #server全局塊
location [PATTERN] #location塊
{
...
}
location [PATTERN]
{
...
}
}
server
{
...
}
... #http全局塊
}
- 全局塊:配置影響nginx全局的指令。一般有運作nginx伺服器的使用者組,nginx程序pid存放路徑,日志存放路徑,配置檔案引入,允許生成worker process數等。
- events塊:配置影響nginx伺服器或與使用者的網絡連接配接。有每個程序的最大連接配接數,選取哪種事件驅動模型處理連接配接請求,是否允許同時接受多個網路連接配接,開啟多個網絡連接配接序列化等。
- http塊:可以嵌套多個server,配置代理,緩存,日志定義等絕大多數功能和第三方子產品的配置。如檔案引入,mime-type定義,日志自定義,是否使用sendfile傳輸檔案,連接配接逾時時間,單連接配接請求數等。
- 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; #制定日志路徑,級别。這個設定可以放入全局塊,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
}
}
}
3、location比對規則
從功能看,rewrite 和 location 似乎很像,都能實作跳轉,主要差別在于 rewrite 是在同一域名内更改擷取資源的路徑,而 location 是對一類路徑做控制通路或反向代理,還可以 proxy_pass 到其他機器。
- 精準比對
location = / {······}
- 一般比對
location / {······}
- 正則比對
location ~ / {······}
比對修飾符
= :精确比對(必須全部相等)
~ :大小寫敏感(正規表達式)
~* :忽略大小寫(正規表達式)這裡要注意忽略大小寫的意思是請求的字元大小寫都可以
^~ :隻需比對uri部分,使用字首比對。如果比對成功,則不再比對其他 location
@ :内部服務跳轉
1、=,精确比對,一般是比對某個具體檔案
http://xxx.com/index.html [比對成功]
location = /index.html {
[ configuration ]
}
2、~,大小寫敏感(正規表達式)
http://xxx.com/yunst/ [失敗]
http://xxx.com/YUNST/ [成功]
location ~ /YUNST/ {
[ configuration ]
}
3、~*,大小寫忽略(正規表達式)
http://xxx.com/yunst/ [成功]
http://xxx.com/YUNST/ [成功]
location ~* /YUNST/ {
[ configuration ]
}
4、^~,隻比對以 uri 開頭,比對成功以後,會停止搜尋後面的正規表達式比對
http://xxx.com/img/1.jpg [成功]
http://xxx.com/img/2.png [成功]
location ^~ /img/ {
[ configuration ]
}
5、比對以gif、jpg、jpeg結尾的檔案
http://xxx.com/img/1.jpg [成功]
http://xxx.com/img/2.png [失敗]
http://xxx.com/img/3.gif [成功]
location ~* \.(gif|jpg|jpeg)$ {
[ configuration ]
}
5、@,nginx内部跳轉
location /data/ {
error_page 404 @img_err;
}
location @img_err {
[ configuration ]
}
4、nginx配置完整模版
xxx.conf
upstream sc{
server 127.0.0.1:8081;
server 127.0.0.1:8082;
}
server {
listen 80;
server_name xxx.com;
rewrite ^(.*)$ https://${server_name}$1 permanent;
}
server {
listen 443 ssl;
server_name xxx.com;
root /opt/www/dist;
charset utf-8;
client_max_body_size 200m;
ssl_certificate cert/xxx.crt;
ssl_certificate_key cert/xxx.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
access_log /var/log/nginx/access.log main;
error_log /var/log/nginx/error.log info;
#公衆号驗證
location ~ ^/MP_verify_([a-zA-Z0-9]+).txt$ {
default_type text/html;
set $str $1;
return 200 $1;
}
location / {
client_max_body_size 200m;
try_files $uri $uri/ /index.html;
index index.html;
}
location ^~ /api/ {
client_max_body_size 200m;
proxy_pass http://sc/api/;
}
}