指令 | 值 |
---|---|
user | 出於安全考慮,默認是nginx、nobody |
worker_processes | 工做進程數,通常來講,設置與CPU的核心數相同便可 |
error_log | 保存錯誤日誌的路徑,能夠設置error_log的級別 |
pid | nginx 進程id |
user nginx; worker_processes 4; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { # 每秒處理多少個客戶端鏈接 worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] ' '"$request" $status $body_bytes_sent ' '"$http_referer" "$http_user_agent"'; access_log /var/log/nginx/access.log main; sendfile on; tcp_nopush on; #與客戶端保持鏈接的最長時間 keepalive_timeout 65; gzip on; include /etc/nginx/conf.d/*.conf; }
日積月累,nginx 的日誌文件也會變得愈來愈大,若是咱們是本身編譯安裝的nginx,可能就須要本身來處理日誌文件的分割了。定時分割日誌文件,這有利於下降單個文件的大小,方便排查,同時只保留最近一段時間的日誌,也能夠節省磁盤空間php
通常採用 logrotate 來進行日誌文件的分割:https://linux.die.net/man/8/l...html
# USR1 re-opening log files http://nginx.org/en/docs/control.html # daily 天天處理一次 # missingok 忽略文件不存在的錯誤 # rotate 設置舊文件保留的數量 # compress 對日誌進行壓縮 # delaycompress 延遲壓縮 # notifempty 若是文件內容爲空,不處理 # create 設置文件權限、用戶、組 # sharedscripts 對於prerotate和postrotate腳本,若是匹配到了多個日誌文件,只運行一次腳本。若是沒有匹配到,則不運行。 # postrotate表示在日誌rotate以後,執行的腳本 /var/log/nginx/*.log { daily missingok rotate 30 compress delaycompress notifempty create 640 nginx nginx sharedscripts postrotate if [ -f /var/run/nginx.pid ]; then kill -USR1 `cat /var/run/nginx.pid` fi endscript }
測試 logrotate 配置是否生效linux
logrotate -v /etc/logrotate.conf
調試定時任務,不會處理實際文件logrotate -vf /etc/logrotate.conf
馬上執行全部任務,無論是否已到執行時間,會處理實際文件nginx
server { listen 80; server_name localhost; #nginx程序默認的web根目錄 location / { root /usr/share/nginx/html; index index.html index.htm; } error_page 404 /404.html; # 重定向服務器的錯誤頁到指定的靜態頁面 /50x.html error_page 500 502 503 504 404 /50x.html; location = /50x.html { root /usr/share/nginx/html; } location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } }
當咱們配置好了Nginx,就可使用curl來進行初步的測試了git
curl -v http://127.0.0.1/ > /dev/null
假若咱們想禁止用戶經過IP或者未設置的域名來訪問,能夠採起以下措施github
server { listen 80 default; rewrite ^(.*) http://www.example.com permanent; }
變量類型 | 變量列表 |
---|---|
內置變量 | http://nginx.org/en/docs/vari... |
HTTP請求變量 | http://nginx.org/en/docs/http... |
參考文檔:http://www.ttlsa.com/nginx/ng...web
若是咱們想要訪問/path/to/name,那麼 Nginx 配置中,路由匹配的優先級以下正則表達式
修飾符 | 說明 | 優先級 |
---|---|---|
location = /path/to/name | 精確匹配 | 1 |
location /path/to/name | 完整匹配 | 2 |
location ^~ /path/to/name | 表示uri以某個常規字符串開頭,非正則表達式 | 3 |
location ~* /path/to/name | 使用不區分大小寫的正則來進行匹配 | 4 |
location ~ /path/to/name | 使用區分大小寫的正則來進行匹配 | 4 |
location /path | 部分匹配 | 5 |
location / | 通用匹配 | 6 |