一.nginx的目錄結構
[root@node nginx_116]# ls
client_body_temp conf fastcgi_temp html logs proxy_temp sbin scgi_temp uwsgi_temp
conf 存放nginx所有配置文件的目錄,主要nginx.conf
html 存放nginx默認站點的目錄,如index.html、error.html等
logs 存放nginx默認日志的目錄,如error.log access.log
sbin 存放nginx主命令的目錄,sbin/nginx
二.nginx主配置文件解析
CoreModule核心模塊
user www; #Nginx進程所使用的用戶 worker_processes 1; #Nginx運行的work進程數量(建議與CPU數量一致或auto) error_log /log/nginx/error.log #Nginx錯誤日志存放路徑 pid /var/run/nginx.pid #Nginx服務運行后產生的pid進程號
events事件模塊
events { worker_connections //每個worker進程支持的最大連接數 use epool; //事件驅動模型, epoll默認 }
http內核模塊
//公共的配置定義在http{} http { //http層開始 ... //使用Server配置網站, 每個Server{}代表一個網站(簡稱虛擬主機) 'server' { listen 80; //監聽端口, 默認80 server_name localhost; //提供服務的域名或主機名 access_log host.access.log //訪問日志 //控制網站訪問路徑 'location' / { root /usr/share/nginx/html; //存放網站代碼路徑 index index.html index.htm; //服務器返回的默認頁面文件 } //指定錯誤代碼, 統一定義錯誤頁面, 錯誤代碼重定向到新的Locaiton error_page 500 502 503 504 /50x.html; } ... //第二個虛擬主機配置 'server' { ... } include /etc/nginx/conf.d/*.conf; //包含/etc/nginx/conf.d/目錄下所有以.conf結尾的文件 } //http層結束
nginx核心配置
#定義nginx工作進程數 worker_processes 5;#錯誤日志 #error_log logs/error.log; #http定義代碼主區域http { include mime.types; default_type application/octet-stream; #定義nginx的訪問日志功能 #nginx會有一個accses.log功能,查看用戶訪問的記錄 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; sendfile on; keepalive_timeout 65; #開啟gzip壓縮傳輸 gzip on; #虛擬主機1 定義一個 斗魚網站 server { #定義nginx的訪問入口端口,訪問地址是 192.168.11.37:80 listen 80; #定義網站的域名www.woshidouyu.tv #如果沒有域名,就填寫服務器的ip地址 192.168.11.37 server_name www.woshidouyu.tv; #nginx的url域名匹配 #只要請求來自於www.woshidouyu.tv/111111111 #只要請求來自於www.woshidouyu.tv/qweqwewqe #最低級的匹配,只要來自於www.woshidouyu.tv這個域名,都會走到這個location location / { #這個root參數,也是關鍵字,定義網頁的根目錄 #以nginx安裝的目錄為相對路徑 /opt/nginx112/html #可以自由修改這個root定義的網頁根目錄 root html; #index參數定義網站的首頁文件名,默認的文件名 index index.html index.htm; } #錯誤頁面的優化(只要是遇到前面4系列的錯誤,就會直接跳轉到相對目錄下的40x.html頁面) error_page 400 401 402 403 404 /40x.html; } }