目錄結構如下:
Nginx基礎知識
Nginx HTTP服務器的特色及優點
Nginx的主要企業功能
Nginx作為web服務器的主要應用場景包括:
Nginx的安裝
安裝環境
快速安裝命令集合
各個命令解釋
腳本
注意
安裝故障總結
故障一:沒有安裝pcre或pcre-devel
故障二:沒有安裝openssl和openssl-devel
常用的Nginx http功能模塊
Nginx的目錄結構
Nginx最重要的配置文件nginx.conf詳解
生產中常見的網站狀態碼
?
Nginx基礎知識:
Nginx HTTP服務器的特色及優點
a. 支持高并發:能支持幾萬并發連接(特別是靜態小文件業務環境)
b. 資源消耗少:在3萬并發連接下,開啟10個Nginx線程消耗的內存不到200MB
PHP中間件,c. 可以做HTTP反向代理及加速緩存,即負載均衡功能,內置對RS節點服務器健康檢查功能,這相當于專業的Haproxy軟件或LVS的功能
d. 具備Squid等專業緩存軟件等的緩存功能
e. 支持異步網絡I/O事件模型epoll
Nginx的主要企業功能
a. 使用Nginx運行HTML,JS,CSS,小圖片等靜態數據(此功能類似Lighttpd軟件)
b. Nginx結合FastCGI運行php等動態程序(例如使用fastcgi_pass方式)
redis方法。c. Nginx結合Tomcat/Resin等支持Java動態程序(常用的proxy_pass)
Nginx作為web服務器的主要應用場景包括:
a. 使用Nginx運行HTML,JS,CSS,小圖片等靜態數據(此功能類似Lighttpd軟件)
b. Nginx結合FastCGI運行php等動態程序(例如使用fastcgi_pass方式)
c. Nginx結合Tomcat/Resin等支持Java動態程序(常用的proxy_pass)
一般情況下普通php引擎支持的并發連接參考為300-1000,Java引擎和數據庫的并發連接參考值為300-1500.當然架構不同可能會有浮動
Nginx的安裝
安裝環境
LINUX教程,a. 查看當前系統cat /etc/redhat-release
[root@nginx /]# cat /etc/redhat-release
CentOS release 6.7 (Final)
[root@nginx /]#
b. 查看系統內核uname –r
NGINX反向代理、[root@nginx /]# uname -r
2.6.32-573.el6.x86_64
[root@nginx /]#
快速安裝命令集合:
1 yum install pcre pcre-devel –y 2 yum install openssl openssl-devel –y 3 useradd nginx -M -s /sbin/nologin 4 ./configure --user=nginx --group=nginx --prefix=/application/nginx1.6.2 --with-http_stub_status_module --with-http_ssl_module 5 make&&make install 6 ln -s /application/nginx1.6.2/ /application/nginx 7 /application/nginx/sbin/nginx –t 8 /application/nginx/sbin/nginx
各個命令解釋
a. 安裝前需要安裝pcre庫(兼容正則表達式)
NGINX負載均衡,yum install pcre pcre-devel –y
b. 還需要安裝openssl
yum install openssl openssl-devel –y
c. 編譯之前還需要創建一個用戶
useradd nginx -M -s /sbin/nologin
php架構設計?d. 編譯安裝:
./configure --user=nginx --group=nginx --prefix=/application/nginx1.6.2? --with-http_stub_status_module? --with-http_ssl_module
make&&make install
e. 安裝完成后的檢查與啟動
/application/nginx/sbin/nginx –t
NGINX面試題?/application/nginx/sbin/nginx
腳本
a. 同樣的可以通過腳本實現整體的安裝(腳本如下)
1 #!/bin/bash 2 . /etc/init.d/functions 3 4 5 nginx_tool_dir=/home/zhaofan/tools 6 nginx_version=1.6.2 7 nginx_install_dir=/application/nginx$nginx_version 8 nginx_ln_dir=/application/nginx 9 10 11 echo "------step1:install pre and openssl-dvel------" 12 yum install pcre pcre-devel openssl openssl-devel -y 13 14 15 16 echo "------step2:addd nginx user------" 17 useradd -s /sbin/nologin -M nginx 18 sleep 1 19 20 echo "------step3:upload nginx software------" 21 22 mkdir -p $nginx_tool_dir 23 cd $nginx_tool_dir 24 [ ! -f nginx-${nginx_version}.tar.gz ] && { 25 echo "you need to upload packet" 26 exit 1 27 } 28 29 30 echo "------step4:install nginx------" 31 tar xf nginx-$nginx_version.tar.gz 32 cd nginx-$nginx_version 33 ./configure --user=nginx --group=nginx --prefix=${nginx_install_dir} --with-http_stub_status_module --with-http_ssl_module 34 35 [ $? -ne 0 ] && 36 { 37 echo "configure is errror" 38 exit 1 39 40 } 41 42 43 make && make install 44 [ $? -ne 0 ] && 45 { 46 echo "make && make install is error" 47 exit 1 48 } 49 ln -s ${nginx_install_dir} ${nginx_ln_dir} 50 51 52 echo "------step5:check and runn nginx------" 53 54 $nginx_ln_dir/sbin/nginx -t 55 $nginx_ln_dir/sbin/nginx 56 57 58 echo ---------- 59 ps -ef|grep nginx 60 echo ---------- 61 lsof -i tcp:80 62 echo ---------- 63 curl 127.0.0.1 64 echo "----------nginx is installed------------"
注意
a. 如果是學習,需要關閉防火墻和selinux,關閉方法如下:
/etc/init.d/iptables stop
setenforce 0臨時關閉)
重啟NGINX、b. 如果想要永久關閉selinux
vi編輯/etc/selinux/config進行下面更改
SELINUX=disabled
c. 也可以通過命令sed直接對命令進行修改
sed -i 's#SELINUX=enable#SELINUX=disabled#g' /etc/selinux/config
按照上述操作啟動成功后,通過瀏覽器打開訪問:
?
安裝故障總結
故障一:沒有安裝pcre或pcre-devel
會提示如下錯誤:
1 ./configure: error: the HTTP rewrite module requires the PCRE library. 2 You can either disable the module by using --without-http_rewrite_module 3 option, or install the PCRE library into the system, or build the PCRE library 4 statically from the source with nginx by using --with-pcre=<path> option.
故障二:沒有安裝openssl和openssl-devel
1 ./configure: error: SSL modules require the OpenSSL library. 2 You can either do not enable the modules, or install the OpenSSL library 3 into the system, or build the OpenSSL library statically from the source 4 with nginx by using --with-openssl=<path> option.
常用的Nginx http功能模塊
?
Nginx http功能模塊 | 模塊說明 |
Ngx_http_core_module | 包括一些核心的http參數配置,對應Nginx的配合為HTTP區塊部分 |
Ngx_http_access_module | 訪問控制模塊,用來控制網站用戶對Nginx的訪問 |
Ngx_http_gzip_module | 壓縮模塊,對Nginx返回的數據壓縮,屬于性能優化模塊 |
Ngx_http_fastcgi_module | FastCGI模塊,和動態應用相關的模塊,例如PHP |
Ngx_http_proxy_module | Proxy 代理模塊 |
Ngx_http_upstream_module | 負載均衡模塊,可以實現網站的負載均衡功能及節點的健康檢查 |
Ngx_http_rewrite_module | URL地址重寫模塊 |
Ngx_http_limit_conn_module | 限制用戶并發連接數及請求數模塊 |
Ngx_http_limit_req_module | 根據定義的key限制Nginx請求過程的速率 |
Ngx_http_log_module | 訪問日志模塊,以指定的格式記錄Nginx客戶訪問日志等信息 |
Ngx_http_auth_basic_module | web認證模塊,設置web用戶通過賬號,密碼訪問Nginx |
Ngx_http_ssl_module | ssl模塊,用于加密的http連接如https |
Ngx_http_stub_status_module | 記錄Nginx基本訪問狀態信息等的模塊 |
Nginx的目錄結構
|-- client_body_temp
|-- conf??????????????????????? #這是Nginx所有配置文件的目錄
|?? |-- fastcgi.conf??????????? ??#fastcgi相關參數的配置文件
|?? |-- fastcgi.conf.default???
|?? |-- fastcgi_params ? ? ? ? ? ? ? #fastcgi的參數文件
|?? |-- fastcgi_params.default
|?? |-- koi-utf
|?? |-- koi-win
|?? |-- mime.types????????????? #媒體類型
|?? |-- mime.types.default??
|?? |-- nginx.conf ? ? ? ? ? ? ? ?#nginx默認的主配置文件
|?? |-- nginx.conf.default
|?? |-- scgi_params???????????? #scgi相關參數
|?? |-- scgi_params.default
|?? |-- uwsgi_params??????????? ?#uwsgi相關參數
|?? |-- uwsgi_params.default
|?? `-- win-utf
|-- fastcgi_temp?????????????? ? #fastcgi臨時數據目錄
|-- html ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?#編譯安裝Nginx的默認站點目錄
|?? |-- 50x.html ? ? ? ? ? ? ? ? ? #錯誤頁面優雅替代顯示文件
|?? `-- index.html ? ? ? ? ? ? ? #默認的首頁文件
|-- logs ? ? ? ? ? ? ? ? ? ? ? ? ? ? #默認的日志路徑包括錯誤日志和訪問日志
|?? |-- access.log
|?? |-- error.log
|?? `-- nginx.pid
|-- proxy_temp????????????????? #臨時目錄
|-- sbin ? ? ? ? ? ? ? ? ? ? ? ? ? ? Nginx命令目錄
|?? `-- nginx ? ? ? ? ? ? ? ? ? ? 啟動命令
|-- scgi_temp?????????????????? #臨時目錄
`-- uwsgi_temp????????????????? #臨時目錄
Nginx最重要的配置文件nginx.conf詳解
通過命令將nginx配置文件精簡化顯示(去掉#注釋和空行的內容):
egrep -v "#|^$" nginx.conf.default >nginx.conf
worker_processes? 1;???????????? #worker進程的數量
events {???????????????????????? #事件區塊的開始
??? worker_connections? 1024;?? ?#每個worker進程支持的最大連接數
}??????????????????????????????? #事件區塊的結束
http {????????????????????????? ?#http區塊的開始
??? include?????? mime.types;???? #nginx支持的媒體類型庫文件
??? default_type? application/octet-stream;? #默認的媒體類型
??? sendfile??????? on;?????????? #開啟高效傳輸模式
??? keepalive_timeout? 65;?????? ?#連接超時
??? server {????????????????????? #第一個server區塊開始,表示一個獨虛擬主機站點
??????? listen?????? 80;????????? #服務端口,默認80
??????? server_name? localhost;? ?#提供服務的域名主機名
??????? location / {????????????? #第一個location區塊開始
??????????? root?? html;????????? #站點的根目錄,相當于Nginx的安裝目錄
??????????? index? index.html index.htm; #默認的首頁文件,如果多個用空格分開
??????? }???????????????????????? #第一個location區塊結束
??????? error_page?? 500 502 503 504? /50x.html; #出現對象http狀態碼時使用50x.html回應用戶
??????? location = /50x.html {
??????????? root?? html;
??????? }
??? }
}??????????????????????????????? ?#http區塊結束
?
生產中常見的網站狀態碼
?
狀態碼 | 詳細描述說明 |
200-OK | 服務器成功返回網頁,這是成功的狀態碼 |
301-Moved Permanently | 永久跳轉,所請求的網頁將永久跳轉到被設定的新位置 |
403-Forbidden | 禁止訪問,雖然這個請求時合法的,但是服務器端因為匹配了預先設置的規則而拒絕相應客戶端的請求,此類問題一般為服務器或服務器權限配置不當所致 |
404-Not Found | 服務器找不到客戶端請求的指定頁面,可能是客戶端請求了服務器上不存在的資源所導致 |
500-Internal Server Error | 內部服務器錯誤,服務器遇到了意料不到的情況,不能完成客戶的請求,這是一個較為籠統的報錯,一般為服務器的設置或內部程序問題導致 |
502-Bad Gateway | 壞的網關,一般是代理服務器請求后端服務時,后端服務不可用或沒有完成相應網關服務器,這通常為反向代理服務器下面的節點出問題導致 |
503-Service Unavailable | 服務當前不可用,可能是服務器超載或停機維護導致的,或者是反向代理沒有可以提供的服務節點 |
504-Gateway Timeout | 網關超時,一般是網關代理服務器請求后端服務時,后端服務沒有在特定的時間內完成處理請求,多數是服務器過載導致沒有在指定的時間內返回數據給前端代理服務器 |