一 、Nginx簡述
Nginx是一個開源、高性能、可靠的HTTP中間件、代理服務。
二 、常見的HTTP服務
1. HTTPD-Apache基金會
2. IIS-微軟
3. GWS-Google
4. Nginx
三、為什么選擇Nginx
原因一:IO多路復用epoll (主要解決了并發性的問題)
注1:多個描述符的I/O操作都能在一個線程內并發交替的順序完成,這就叫做I/O多路復用,這里的“復用”指的是復用同一個線程。
注2:IO多路復用的實現方式有 select 、poll、epoll (逐漸進化)
原因二:輕量級
1、功能模塊少(只保留了HTTP和其相關核心功能的模塊)
2、代碼模塊化??
原因三:? CPU親和(affinity)
注:cpu親和是一種把cpu核心和Nginx工作進程綁定的方式,把每個worker進程固定在一個cpu上執行,減少切換cpu的cache,miss,獲得更好的性能。
原因四:sendfile工作機制
四、Nginx 快速安裝
注:Nginx官方下載頁面:http://nginx.org/en/download.html
1. 編輯nginx yum源
vim /etc/yum.repos.d/nginx.repo
2. 將下面內容復制到這個文件上,然后保存。
[nginx] name=nginx repo baseurl=http://nginx.org/packages/centos/7/$basearch/ gpgcheck=0 enabled=1
3. 執行yum安裝??
yum -y install nginx
reboot? #重啟系統
4. 確認nginx安裝完成
nginx -v
注:
安裝后nginx文件夾映射在本地windows上的目錄如下
將Linux上文件映射到本地windows上教程:?https://blog.csdn.net/m_nanle_xiaobudiu/article/details/80631437
五 Nginx 目錄和配置語法
1. nginx安裝目錄
命令:rpm -ql nginx
2. Nginx 的配置語法(nginx.conf)
原始nginx.conf
user nginx; worker_processes 1; 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" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; include /etc/nginx/conf.d/*.conf; }
參數解釋:
user:設置nginx服務的系統使用用戶(默認為nginx,非root)【更安全】
worker_processes: 工作進程數【最好與cpu核心數保持一致】
error_log:nginx的錯誤日志定義
pid:nginx服務啟動時的pid【存放nginx pid的位置】
events部分:
worker_connections? 每個進程允許最大連接數【一般調節到一萬左右】
http部分:
logformat:定義日志類型
keeplive_timeout:設置客戶端和服務端連接超時的時間
access_log:? 訪問日志
include /etc/nginx/conf.d/*.conf: 子配置文件( 大網站多站點可以在這里進行分開配置 )
3. 子配置文件格式
默認只有一個default.conf,當網站存在多個域名地址時,可以在子配置文件進行分開配置,比如這樣
這樣,nginx首先讀取nginx.conf 配置文件, 我們在nginx.conf配置文件中進行一些基礎公共部分的定義,然后在nginx.conf最末尾又include了conf.d文件夾向下的所有.conf文件
我們在conf.d文件夾下定義每一個站點域名的配置(比如包括網站根路徑,重寫規則,錯誤日志位置定義,訪問日志位置定義等等,總之所有關于SERVER的定義通通在子配置文件中進行定義,而不是在nginx.conf中定義),比如下面這樣
總結:?
1. 在nginx.conf中定義基礎公共部分配置
2. 在conf.d文件夾下分別定義每一個server的配置
3. 當然,習慣用phpstudy的同學也可以在nginx.conf文件末尾再include vhost.conf文件,也是可以的
server { listen 80; server_name test.passport.kk.com; charset utf-8; access_log logs/access/test.passport.kk.com.access.log main; error_log logs/error/test.passport.kk.com.log debug; root "G:/Faraway/data/passport/htdocs"; index index.html index.htm index.php; location /favicon.ico { log_not_found off; access_log off; }location / { if (!-e $request_filename) { rewrite ^/(.*)$ /index.php/$1 last; }}location ~ \.php(.*)$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_split_path_info ^((?U).+\.php)(/?.+)$; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info; include fastcgi_params; }location /doc { if (!-f $request_filename){ rewrite ^/doc/(.*)$ /doc/index.php/$1 last; }}location /api { if (!-f $request_filename){ rewrite ^/api/(.*)$ /api/index.php/$1 last; }}error_page 500 502 503 504 /50x.html; location = /50x.html { root html; }}
Nginx初級 好文推薦?https://blog.csdn.net/u012486840/article/details/53098890