一,Nginx基本介紹
1,nginx概念
Nginx 是一款輕量級、高性能的服務器軟件,核心能力是 “處理網絡請求”,被廣泛用于網站、App 的后端架構中。
Nginx 就像一個 “高效的網絡交通指揮官”,核心價值是用最少的資源,處理最多的請求,讓整個系統又快又穩。無論是小網站還是像淘寶、抖音這樣的巨頭,幾乎都離不開它 —— 它是現代互聯網架構的 “基礎設施” 之一。
(與瀏覽器為兩端
2,nginx優勢
- 高并發:事件驅動模型 + 單線程 Worker 進程,單臺服務器可支撐數萬并發連接;
- 高可靠:Master-Worker 進程隔離,Worker 故障自動重啟,服務可用性高;
- 高性能:非阻塞 I/O 減少等待,內存占用低(通常幾 MB 到幾十 MB);
- 高擴展:模塊化設計支持按需擴展,第三方模塊豐富;
- 易運維:支持平滑重載配置、版本升級,運維操作不中斷服務。
二,nginx主配置文件
/usr/local/nginx/conf/nginx.conf
[root@web1 conf]# cat nginx.conf
1,基本信息
#user ?nobody;? ? ? ? 使用的nginx賬號
worker_processes ?1;? ? ? ? 工作進程數量(看CPU核數量
#error_log ?logs/error.log;? ? ? ? 錯誤日志配置
#error_log ?logs/error.log ?notice;? ? ? ? notice級別
#error_log ?logs/error.log ?info;? ? ? ? info級別
#pid ? ? ? ?logs/nginx.pid;? ? ? ? pid文件路徑
? ? ? ?events { }事件模塊
events {
worker_connections ?1024;? ? ? ? 最大并發數 1024
}
2,http模塊
? ? ? ? http { }模塊,處理HTTP協議
http {
include ? ? ? mime.types;? ? ? ? 引入MINE類型映射文件
default_type ?application/octet-stream;? ? ? ? 默認
? ? #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;?????????開啟 sendfile 機制(零拷貝技術),加速靜態文件(如圖片、HTML)的傳輸效率
? ? #tcp_nopush ? ? on;? ? ? ? 網絡傳輸次數
? ? #keepalive_timeout ?0;? ? ? ? 關閉長連接
keepalive_timeout ?65;? ? ? ? 65秒長連接
? ? #gzip ?on;? ? ? ? GZIP壓縮
? ? ? ? ? ? ? ? server{ } 模塊,在http模塊?虛擬主機配置
? ? server {
listen ? ? ? 80;? ? ? ? 監聽的端口
server_name ?localhost;? ? ? ? 匹配的域名
? ? ? ? #charset koi8-r;? ? ? ? 字符集配置
? ? ? ? #access_log ?logs/host.access.log ?main;? ? ? ? 該虛擬主機的訪問日志
? ? ? ? location / {? ? ? ? 匹配的根路徑
root ? html;? ? ? ? 網站根目錄
index ?index.html index.htm;? ? ? ? 默認首頁文件
}
? ? ? ? #error_page ?404 ? ? ? ? ? ? ?/404.html;? ? ? ? 404錯誤跳轉頁面
? ? ? ? # redirect server error pages to the static page /50x.html????????
#
error_page ? 500 502 503 504 ?/50x.html;? ? ? ? 服務錯誤(500/501/502/503/504
location = /50x.html {? ? ? ? 精確·匹配 /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
#? ? ? ? ? ? ? ? PHP 相關配置,對接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
#????????禁止訪問 .htaccess 文件(默認注釋,若與 Apache 共用目錄時啟用)
#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;
# ? ?}
#}
3,https模塊
? ? ? ? ? ? ? ? ? ? ? ? HTTPS 服務示例
# HTTPS server
#
#server {
# ? ?listen ? ? ? 443 ssl;? ? ? ? 監聽 443 端口 (HTTPS默認端口 并啟用SSL
# ? ?server_name ?localhost;
? ? # ? ?ssl_certificate ? ? ?cert.pem;? ? ? ? SSL 證書文件(公鑰
# ? ?ssl_certificate_key ?cert.key;? ? ? ? SSL 私鑰文件
? ? # ? ?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;
# ? ?}
#}
}
三,nginx的目錄結構
[root@dns-nfs conf]# cd /usr/local/nginx8
[root@dns-nfs nginx8]# ls
client_body_temp conf fastcgi_temp html logs proxy_temp sbin scgi_temp uwsgi_temp
[root@dns-nfs nginx8]#
1,client_body_temp
:客戶端請求體臨時存儲目錄
2,conf:核心目錄
root@load2 conf]# ls
fastcgi.conf fastcgi_params.default mime.types nginx.conf.default uwsgi_params
fastcgi.conf.default koi-utf mime.types.default scgi_params uwsgi_params.default
fastcgi_params koi-win nginx.conf scgi_params.default win-utf
[root@load2 conf]#
nginx.conf????????Nginx 主配置文件
mine.types????????MIME 類型映射文件
koi-utf win????????字符集轉換映射表
(.default是默認備份文件
3,html?默認靜態資源根目錄
(快速部署靜態服務)
[root@load2 nginx8]# cd html
[root@load2 html]# ls
50x.html index.html
[root@load2 html]#
?index.html nginx 默認歡迎頁,內容為 “Welcome to nginx!”,是 Nginx 安裝完成后,訪問?http://服務器IP
?或?http://localhost
?時默認返回的頁面;
50x.html nginx默認錯誤頁面
(可以在nginx主配置文件server模塊修改
server {listen 80;server_name www.my-static.com; # 自定義域名root /var/www/my-static; # 自定義靜態資源目錄(需手動創建)index index.html index.htm; # 默認首頁優先級
}
4,logs 日志核心目錄
[root@load2 logs]# ls
access.log error.log nginx.pid
[root@load2 logs]#
access.log 訪問日志
error.log 錯誤日志
nginx.pid 進程 ID 文件(管理 Nginx 服務) pi進程標識符
5,sbin
[root@load2 nginx8]# cd sbin
[root@load2 sbin]# ls
nginx
[root@load2 sbin]#
編譯安裝的
.nginx啟動服務