天天看點

nginx基礎知識(掌握)

作者:軟體開發資料查詢網

前言

Nginx是一個高性能的HTTP和反向代理伺服器,特點是占用記憶體少,并發能力強,事實上nginx的并發能力确實在同類型的網頁伺服器中表現較好。

nginx專為性能優化而開發,性能是其最重要的要求,十分注重效率,有報告nginx能支援高達50000個并發連接配接數

nginx安裝

#nginx安裝
yum -y install gcc pcre-devel openssl-devel			#依賴包
useradd -s /sbin/nologin  nginx						
./configure
--prefix=/usr/local/nginx							#指定安裝目錄
--user=nginx										#指定使用者
--with-http_ssl_module								#開啟加密功能
make && make install								#編譯及安裝           

nginx腳本啟動

/usr/local/nginx/sbin/nginx							#啟動
/usr/local/nginx/sbin/nginx	 -s stop				#關閉
/usr/local/nginx/sbin/nginx  -s reload				#從新加載配置
							-V						#檢視軟體資訊
							-t						#測試配置檔案           

nginx檔案

/usr/local/nginx/html								#測試頁面
#nginx配置檔案
#Nginx的預設通路日志檔案為/usr/local/nginx/logs/access.log
#Nginx的預設錯誤日志檔案為/usr/local/nginx/logs/error.log
#PHP預設錯誤日志檔案為/var/log/php-fpm/www-error.log
#-with-http_ssl_module參數,啟用加密子產品,對于需要進行SSL加密處理的站點添加ssl相關指令(設定網站需要的私鑰和證書
nginx/conf/nginx.conf
server {
		listen		1.1.1.1:80								#監聽IP位址與端口
        listen       80;									#監聽端口
        server_name  localhost;								#網站域名
#位址重寫格式【總結】
#rewrite 舊位址 新位址 [選項];
#last 不再讀其他rewrite
#break 不再讀其他語句,結束請求
#redirect 臨時重定向
#permanent 永久重定向
        rewrite  /a.html /b.html redirect;					#将a網頁重定向到b網頁并位址跳轉
        rewrite ^/ http://www.baidu.com;					#通路此位址全部從定向到baidu
		rewrite ^/(.*)$  http://www.baidu.com/$1;			#通路此位址下面的網頁從定向baidu
        ssl_certificate      cert.pem;         				#這裡是證書檔案
        ssl_certificate_key  cert.key;        				#這裡是私鑰檔案
        auth_basic "Input Password:";                       #認證提示符資訊
        auth_basic_user_file  "/usr/local/nginx/pass";      #認證的密碼檔案
        location / {
            root   html;									#指定網站根路徑
            index  index.html index.htm;
        }
	#這裡,~符号代表正則比對,*符号代表不區分大小寫
	if ($http_user_agent ~* firefox) {           		    #識别用戶端firefox浏覽器
	rewrite ^(.*)$  /firefox/$1;
	}
  }           

lnmp環境

#部署lnmp平台解決動态資料,動态資料為網站解析代碼後傳回資料
yum -y install gcc openssl-devel pcre-devel						#nginx依賴環境
yum -y install php php-mysql php-fpm							#php及依賴環境
yum -y install   mariadb   mariadb-server   mariadb-devel		#mariadb及依賴環境
systemctl start mariadb php-fpm

#php-fpm配置檔案
/etc/php-fpm.d/www.conf
listen = 127.0.0.1:9000											#php端口号
pm.max_children = 32											#最大程序數
pm.start_servers = 15											#最小程序數

#修改nginx配置檔案
nginx/conf/nginx.conf
location / {
            root   html;
            index  index.php  index.html   index.htm;
#設定預設首頁為index.php,當使用者在浏覽器位址欄中隻寫域名或IP,不說通路什麼頁面時,伺服器會把預設首頁index.php傳回給使用者
        }
 location  ~  \.php$  {
            root           html;
            fastcgi_pass   127.0.0.1:9000;    #将請求轉發給本機9000端口,PHP解釋器
            fastcgi_index  index.php;
            include        fastcgi.conf;       #加載其他配置檔案
        }           

nginx實作web反向代理

一;高可用一台當機,服務不會停,

二;負載均衡(輪詢,哈希,權重,随機等)。 分布式;各個伺服器執行不同功能,來完成一件事

/nginx/conf/nginx.conf
.. ..
http {
.. ..
#使用upstream定義後端伺服器叢集,叢集名稱任意(如webserver)
#使用server定義叢集中的具體伺服器和端口
upstream webserver {
#通過ip_hash設定排程規則為:相同用戶端通路相同伺服器
                ip_hash;
                server 192.168.2.100 weight=1 max_fails=1 fail_timeout=30;
                server 192.168.2.200 weight=2 max_fails=2 fail_timeout=30;
                server 192.168.2.101 down;
        }
#weight設定伺服器權重值,預設值為1
#max_fails設定最大失敗次數,測試伺服器幾次才确認伺服器失敗
#fail_timeout設定失敗逾時時間,機關為秒
#down标記伺服器已關機,不參與叢集排程
.. ..
server {
        listen        80;
        server_name  localhost;
            location / {
#通過proxy_pass将使用者的請求轉發給webserver叢集
            proxy_pass http://webserver;
        }           

nginx的TCP/UDP 排程器 Nginx編譯安裝時需要使用--with-stream,開啟ngx_stream_core_module子產品

stream {
            upstream backend {
               server 192.168.2.100:22;           #後端SSH伺服器的IP和端口
               server 192.168.2.200:22;
}
            server {
                listen 12345;                    #Nginx監聽的端口
                 proxy_pass backend;
             }
}           

nginx常見問題

#自定義錯誤頁面
nginx/conf/nginx.conf
.. ..
charset utf-8;                    #僅在需要中文時修改該選項
error_page   404  /404.html;      #自定義錯誤頁面
.. ..
nginx/html/404.html			      #定義錯錯誤頁面
#200			一切正常
#301			永久從定向
#302			臨時從定向
#401			使用者或密碼錯誤
#403			禁止通路(用戶端IP位址被拒接)
#404			檔案不存在
#414			請求URL頭過長
#500			伺服器内部錯誤
#501			bad gateway
           

檢視伺服器狀态 編譯安裝時使用--with-http_stub_status_module開啟狀态頁面子產品

… …
location /status {
                stub_status on;
                 #allow IP位址;
                 #deny IP位址;
        }
… …
curl  http://192.168.4.5/status
Active connections: 1 
server accepts handled requests
 10 10 3 
Reading: 0 Writing: 1 Waiting: 0
#Active connections:目前活動的連接配接數量。
#Accepts:已經接受用戶端的連接配接總數量。
#Handled:已經處理用戶端的連接配接總數量。(一般與accepts一緻,除非伺服器限制了連接配接數量)。
#Requests:用戶端發送的請求數量。
#Reading:目前伺服器正在讀取用戶端請求頭的數量。
#Writing:目前伺服器正在寫響應資訊的數量。
#Waiting:目前多少用戶端在等待伺服器的響應           

優化nginx并發量

ad -n 2000 -c 2000 http://192.168.1.10 		#ad高并發測試
nginx/conf/nginx.conf
.. ..
worker_processes  2;                    	#與CPU核心數量一緻
events {
worker_connections 65535;       		    #每個worker最大并發連接配接數
}
.. ..
#優化Linux核心參數(最大檔案數量)
ulimit -a									#檢視所有屬性值
ulimit -Hn	100000							#設定硬限制(臨時規則)
ulimit -Sn	100000							#設定軟限制(臨時規則)
vim  /etc/security/limits.conf
  .. ..
*               soft    nofile            100000
*               hard    nofile            100000
#該配置檔案分4列,分别如下:
#使用者或組    硬限制或軟限制    需要限制的項目   限制的值           

優化nginx資料標頭緩存

.. ..
http {
client_header_buffer_size    1k;        #預設請求標頭資訊的緩存    
large_client_header_buffers  4 4k;      #大請求標頭部資訊的緩存個數與容量
.. ..
}           

浏覽器本地緩存靜态資料

server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ {
expires        30d;            #定義用戶端緩存時間為30天
}
}           

繼續閱讀