Nginx 基础(一)
一 、Nginx簡述
Nginx是一個開源、高性能、可靠的HTTP中間件、代理服務(wù)。
二 、常見的HTTP服務(wù)
1. HTTPD-Apache基金會
2. IIS-微軟
3. GWS-Google
4. Nginx
三、為什么選擇Nginx
原因一:IO多路復(fù)用epoll (主要解決了并發(fā)性的問題)
注1:多個描述符的I/O操作都能在一個線程內(nèi)并發(fā)交替的順序完成,這就叫做I/O多路復(fù)用,這里的“復(fù)用”指的是復(fù)用同一個線程。
注2:IO多路復(fù)用的實現(xiàn)方式有 select 、poll、epoll (逐漸進(jìn)化)
原因二:輕量級
1、功能模塊少(只保留了HTTP和其相關(guān)核心功能的模塊)
2、代碼模塊化??
原因三:? CPU親和(affinity)
注:cpu親和是一種把cpu核心和Nginx工作進(jìn)程綁定的方式,把每個worker進(jìn)程固定在一個cpu上執(zhí)行,減少切換cpu的cache,miss,獲得更好的性能。
原因四:sendfile工作機(jī)制
四、Nginx 快速安裝
注:Nginx官方下載頁面:http://nginx.org/en/download.html
1. 編輯nginx yum源
vim /etc/yum.repos.d/nginx.repo
2. 將下面內(nèi)容復(fù)制到這個文件上,然后保存。
[nginx] name=nginx repo baseurl=http://nginx.org/packages/centos/7/$basearch/ gpgcheck=0 enabled=1
3. 執(zhí)行yum安裝??
yum -y install nginx
reboot? #重啟系統(tǒng)
4. 確認(rèn)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; }
參數(shù)解釋:
user:設(shè)置nginx服務(wù)的系統(tǒng)使用用戶(默認(rèn)為nginx,非root)【更安全】
worker_processes: 工作進(jìn)程數(shù)【最好與cpu核心數(shù)保持一致】
error_log:nginx的錯誤日志定義
pid:nginx服務(wù)啟動時的pid【存放nginx pid的位置】
events部分:
worker_connections? 每個進(jìn)程允許最大連接數(shù)【一般調(diào)節(jié)到一萬左右】
http部分:
logformat:定義日志類型
keeplive_timeout:設(shè)置客戶端和服務(wù)端連接超時的時間
access_log:? 訪問日志
include /etc/nginx/conf.d/*.conf: 子配置文件( 大網(wǎng)站多站點可以在這里進(jìn)行分開配置 )
3. 子配置文件格式
默認(rèn)只有一個default.conf,當(dāng)網(wǎng)站存在多個域名地址時,可以在子配置文件進(jìn)行分開配置,比如這樣
這樣,nginx首先讀取nginx.conf 配置文件, 我們在nginx.conf配置文件中進(jìn)行一些基礎(chǔ)公共部分的定義,然后在nginx.conf最末尾又include了conf.d文件夾向下的所有.conf文件
我們在conf.d文件夾下定義每一個站點域名的配置(比如包括網(wǎng)站根路徑,重寫規(guī)則,錯誤日志位置定義,訪問日志位置定義等等,總之所有關(guān)于SERVER的定義通通在子配置文件中進(jìn)行定義,而不是在nginx.conf中定義),比如下面這樣
總結(jié):?
1. 在nginx.conf中定義基礎(chǔ)公共部分配置
2. 在conf.d文件夾下分別定義每一個server的配置
3. 當(dāng)然,習(xí)慣用phpstudy的同學(xué)也可以在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
總結(jié)
以上是生活随笔為你收集整理的Nginx 基础(一)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux上搭建Samba,实现wind
- 下一篇: Nginx 基础 ( 二)