首先 我們要有一個源碼安裝的nginx服務器環境
1.首先安裝nginx的所需依賴
yum -y install gcc pcre-devel zlib-devel
gcc 是用來編譯
pcre-devel 正則匹配
zlib-devel 啓用壓縮功能的時候 需要此模塊的支持
2.獲取nginx的源碼包 下載到指定目錄 /usr/local/src
wget http://nginx.org/download/nginx-1.16.1.tar.gz
3.解壓nginx的源碼包
cd /usr/local/src/nginx-1.16.1.tar.gz
4.編譯安裝
cd /usr/local/src/nginx-1.16.1 && ./configure && make && make install
以上只是最基礎的源碼安裝nginx 可以作爲初學者的參考
今天我們主要來詳細解讀一下nginx的配置文件 源碼安裝的nginx的配置文件路徑 cat /usr/local/nginx/conf/nginx.conf (cat命令是查看一個文件)#user nobody; #設置用戶組
worker_processes 1; #啓動子進程數 可以通過 ps aux | grep nginx 查看
#錯誤日誌文件以及日誌級別 notice,info
#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; #每個進程可以處理的連接數 收系統文件句柄限制 ulimit查看
}
http {
include mime.types; #mime.types 爲文件類型指定文件
default_type application/octet-stream; #默認文件類型
#log_format 自定義入職格式 名稱爲mian
#log_format main '$remote_addr - time_local] “KaTeX parse error: Expected 'EOF', got '#' at position 16: request" '
#̲ …status http_referer” ’
# ‘“http_x_forwarded_for”’;
#access_log logs/access.log main; #創建訪問日誌 採用main定義的格式
sendfile on; #是否調用sendfile() 進行數據複製 sendfile複製數據實在內核級別完成的 迴避一般的read write更加高校
#tcp_nopush on;
#keepalive_timeout 0; #保持連接的時
keepalive_timeout 65; #保持連接的超時時間
#gzip on; #是否採用壓縮此功能 講頁面壓縮後傳輸更節省流量
#使用server定義虛擬主機
server {
listen 80; #服務器監聽的端口
server_name localhost; #訪問域名
#charset koi8-r; #編碼格式 如果頁面編碼於此設置不同 講被自動轉碼
#access_log logs/host.access.log main; #設置虛擬主機的訪問日誌路徑及格式
# 對URL井陘匹配
location / {
root html; #root表示網頁根路徑 使用的是相對路徑 html在nginx安裝路徑下
index index.html index.htm; #定義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
#訪問乙php結尾的頁面 則自動講該請求轉至127.0.0.1:80,通過proxy_pass可以實現代理功能
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#這裏可以在做lnmp架構時支持php 可以講註釋解開 做部分修改
#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
#
#拒絕所有人訪問.ht頁面
#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; #監聽TLS使用(TLS:安全傳輸層協議)
# server_name localhost;
# ssl on; #開啓ssl功能
# ssl_certificate cert.pem; #指定證書文件,相對路徑 文件需要放在nginx.conf同目錄下
# ssl_certificate_key cert.key; #指定私鑰文件 相對路徑 文件需要放在nginx.conf同目錄下
# 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;
# }
#}
}