1. 程式人生 > 實用技巧 >Nginx--安裝&&配置檔案

Nginx--安裝&&配置檔案

官網:http://nginx.org/en/download.html

nginx版本:1.18

一安裝

 1 下載預編譯環境(預編譯報錯需要安裝什麼庫 直接在庫名後面接 -devel使用yum下載)

yum -y install pcre-devel openssl-devel gcc*

 2建立使用者,解壓包

useradd -M -r -s /sbin/nologin nginx
tar xvf nginx-1.18.0.tar.gz

 3預編譯

[root@localhost ~]# cd nginx-1.18.0/
[root@localhost nginx-1.18.0]# ./configure --with-http_realip_module --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module

  編譯選項

--prefix=path 指定安裝路徑,預設/usr/local/
--sbin-path=path 指定二進位制命令的路徑
--conf-path=path
指定配置檔案conf路徑
--error-log-path= 指定錯誤日誌error.log路徑,預設/usr/local/nginx/logs/
--http-log-path= 指定主日誌access.log路徑
--with-http_stub_status_module 啟用service status頁,預設不啟用
--with-http_ssl_module 啟用ssl模組,以支援https請求
--with-http_realip_module 獲取客戶端真實ip時使用

 

 4編譯安裝

make && make install

 5命令

/usr/local/nginx/sbin/nginx -s 給主程序傳送訊號.可接 stop | quit | reopen | reload(不關閉服務重置配置檔案)
/usr/local/nginx/sbin/nginx -t 檢查主配置檔案有無錯誤
/usr/local/nginx/sbin/nginx -v 檢視版本號
/usr/local/nginx/sbin/nginx -V 檢視版本號及編譯選項
/usr/local/nginx/sbin/nginx -c 指定配置檔案,預設為 conf/nginx.conf

#設定命令
ln -s /usr/local/nginx/sbin/* /usr/local/sbin

二配置檔案詳解(/usr/local/nginx/conf/nginx.conf)

 nginx配置檔案主要分為五大塊

  全域性塊(main)  配置影響nginx全域性的指令,一般有nginx伺服器的使用者組,nginx程序pid存放路徑,日誌存放路徑,配置檔案引入,允許生成worker process等

  events  配置影響nginx伺服器與使用者的網路連線,每個程序的最大連線數,選取哪種事件驅動模型處理連線請求,是否允許同時接受多個網路連線,開啟多個網路連線序列化等

  http   可以巢狀多個server 配置代理、快取、日誌定義等絕大多數功能和第三方模組的配置

  server  配置虛擬主機的相關引數,一個httpd中可以有多個sercer

  location  配置請求的路由,以及各種頁面的處理情況

[root@localhost nginx]# cat /usr/local/nginx/conf/nginx.conf

#user  nobody;             #nginx使用者及組,如果使用者和組名一樣可只寫一個,一般寫作 user nginx;
worker_processes  1;         #工作程序的數量,不要超過8,一般為cpu核數,不確定時設為auto(自動檢測)最優值取決很多因素,包括但不限於CPU核的數量,儲存資料的硬碟,數量及負載模式

#error_log  logs/error.log;      #錯誤日誌的相關資訊(級別 路徑...)一般不用改動
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;    #每個程序的最大連線數,根據需要調整大小
}


http {
   autoindex on;           #新增此行,訪問目錄下沒有索引檔案,則顯示目錄下所有內容 include mime.types;    #副檔名與檔案型別對映表(引用檔案) default_type application
/octet-stream;    #預設應用型別
   server_tokens off;        #新增此行,隱藏版本號 #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 logs/access.log main;                           #定義日誌檔案(可以用include 路徑/檔名) sendfile on;        #開啟高效檔案傳輸模式 #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65;      #連線超時時間,長連線,一般開啟 #gzip on;             #壓縮傳輸 server {               #http子塊 每一個server都是一臺虛擬主機 listen 80;       #監聽埠 server_name localhost;   #虛擬主機域名 #charset koi8-r;       #字符集 #access_log logs/host.access.log main;    #虛擬主機級別日誌 location / {                     #url瀏覽器輸入的路徑
root html;                  #訪問的目錄/usr/local/nginx/html index index.html index.htm;         #索引檔案 } #error_page
404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html;    #錯誤頁面 location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }

三location

 語法  location [修飾符] /uri|pattern {...}

 修飾符  =   精確匹配,優先順序最高

      ^~  字首匹配,優先順序高於正則匹配

      ~   正則匹配,區分大小寫

      ~*  正則匹配,不區分大小寫

         沒有修飾符,優先順序最低

 注:

  優先順序的高低與location出現順序無關

  儘量使用單一的location修飾符去完成任務

  如果uri為字首目錄,動作為proxy_pass 代理的IP後面有目錄,字首目錄會被覆蓋掉

四日誌(內嵌變數)

 更多內嵌變數參見:http://tengine.taobao.org/nginx_docs/cn/docs/http/ngx_http_core_module.html#variables

$remote_addr 記錄直接與伺服器通訊的客戶端的IP
$remote_user 記錄遠端客戶端的名字
$time_local 記錄訪問時間
$request 記錄請求的URL(資源定位)
$status 記錄請求狀態
$body_bytes_sent 記錄傳送給客戶端的檔案的內容的大小
$http_referer 記錄從哪個頁面連結訪問過來的
$http_user_agent 記錄客戶端瀏覽器的資訊
$http_x_forwarded_for 記錄從請求報文首部的X-Forwarded-For欄位獲取的值
$http_x_real_ip 記錄從請求報文首部的X-Real-IP欄位獲取值
$realip_remote_addr 最後一個反向代理伺服器的IP