Nginx
一. Nginx 特性
二. Nginx 基本架構
三. Nginx 基本功能
四. Nginx 安裝
五. Nginx 配置文件
六. Nginx http服務功能測試
七. Nginx LNMP
一. Nginx 特性
-
模塊化,目前只能將模塊編譯進Nginx,暫時不支持動態裝卸載模塊.(httpd優勢)
-
可靠性,一個主進程(master)控制多個工作進程(worker),工作進程響應用戶多個請求(httpd劣勢)
-
低內存消耗,(httpd劣勢)
-
支持熱部署,(httpd一樣)
-
支持事件驅動I/O,AI/O,支持mmap(httpd2.4才算支持event,劣勢)
二. Nginx 基本架構
Nginx由一個master進程生成多個worker進程,每個worker進程接收用戶請求,支持sendfile,AIO,mmap,
三. Nginx 基本功能
-
靜態資源WEB服務器,能緩存打開的文件描述符
-
http,反向代理服務器,緩存服務器,負載均衡服務器
-
支持fastcgi(php),uwsgi(python)與動態程序結合
-
支持ssl傳輸
-
支持虛擬主機
-
支持keepalive
-
支持平滑升級
-
支持定制日志,日志緩存
-
支持url重寫
-
支持路徑別名
-
支持限速,并發控制
-
look look www.nginx.org
四. Nginx 安裝
~]# yum install pcre-devel openssl-devel -y ~]# groupadd -r nginx ~]# useradd -r -g nginx -s /sbin/nologin -M nginx ~]# cd /usr/src/ src]# tar xf nginx-1.8.1.tar.gz src]# cd nginx-1.8.1 nginx-1.8.1]# ./configure --prefix=/usr/local/nginx \ --conf-path=/etc/nginx/nginx.conf \ --user=nginx --group=nginx \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --pid-path=/var/run/nginx/nginx.pid \ --lock-path=/var/lock/nginx.lock \ --with-http_ssl_module \ --with-http_stub_status_module \ --with-http_gzip_static_module \ --with-http_flv_module \ --with-http_mp4_module \ --http-client-body-temp-path=/var/tmp/nginx/client \ --http-proxy-temp-path=/var/tmp/nginx/proxy \ --http-fastcgi-temp-path=/var/tmp/nginx/fastcgi nginx-1.8.1]# make && make install nginx-1.8.1]# mkdir -pv /var/tmp/nginx/{client,proxy,fastcgi} nginx-1.8.1]# cd /usr/local/nginx nginx]# ll #才兩個目錄,當然,我們的配置文件和log文件存放在其他目錄,可見nginx多輕量級 total 8 drwxr-xr-x 2 root root 4096 Jun 24 23:34 html drwxr-xr-x 2 root root 4096 Jun 24 23:34 sbin nginx]# cd ~ ~]# echo "export PATH=/usr/local/nginx/sbin:$PATH" > /etc/profile.d/nginx.sh #準備nginx程序執行是的環境變量 ~]# mkdir .vim ~]# cp -ra /usr/src/nginx-1.8.1/contrib/vim/* .vim/ #設置nginx配置文件的語法著色, ~]# . /etc/profile.d/nginx.sh ~]# nginx #啟動nginx
五. nginx配置文件
~]# cd /etc/nginx/ [root@node2 nginx]# ll total 60 -rw-r--r-- 1 root root 1034 Jun 24 07:21 fastcgi.conf #FPM配置文件段 -rw-r--r-- 1 root root 1034 Jun 24 07:21 fastcgi.conf.default -rw-r--r-- 1 root root 964 Jun 24 07:21 fastcgi_params -rw-r--r-- 1 root root 964 Jun 24 07:21 fastcgi_params.default -rw-r--r-- 1 root root 2837 Jun 24 07:21 koi-utf -rw-r--r-- 1 root root 2223 Jun 24 07:21 koi-win -rw-r--r-- 1 root root 3957 Jun 24 07:21 mime.types #mime類型配置文件 -rw-r--r-- 1 root root 3957 Jun 24 07:21 mime.types.default -rw-r--r-- 1 root root 2656 Jun 24 07:21 nginx.conf #nginx主配置文件 -rw-r--r-- 1 root root 2656 Jun 24 07:21 nginx.conf.default -rw-r--r-- 1 root root 596 Jun 24 07:21 scgi_params -rw-r--r-- 1 root root 596 Jun 24 07:21 scgi_params.default -rw-r--r-- 1 root root 623 Jun 24 07:21 uwsgi_params #uwsgi配置文件段 -rw-r--r-- 1 root root 623 Jun 24 07:21 uwsgi_params.default -rw-r--r-- 1 root root 3610 Jun 24 07:21 win-utf nginx的配置文件主要分為兩段:main http main主要是控制nginx程序的運行 http主要是操作web服務器,且http配置段下還可以繼續分為server段(虛擬主機) 配置文件分段即可把分段的配置文件從主文件剝離出來,形成單獨的配置文件,并且只要在主配置文件中include此配置文件即可,比如說server虛擬主機配置文件段 ================================ 主配置段: user nginx nginx; #worker進程運行的屬主與屬組 worker_processes 3; #worker進程數,建議與CPU核心數少1 pid /var/run/nginx/nginx.pid; #指定pid文件位置 worker_rlimit_nofile 50000; #顯示所有worker進程所能打開的文件數目 worker_cpu_affinity 0001 0010 0100; #綁定CPU,八核cpu就8個0, events { worker_connections 10240; #每個worker進程所能并發處理的連接數 } http配置段: include mime.types; #包含mime配置文件所有配置內容 sendfile on; #開啟sendfile功能 keepalive_timeout 65; #開啟長連接 server配置段: listen 80; #監聽端口 server_name localhost; #服務器名字 location{} #針對匹配到不同的url可以下發不同的配置段 gzip on; #開啟gzip壓縮
六. Nginx http服務功能測試
-
root path
-
location [ = | ~ | ~* | ^~ ] uri { … }
-
alias path
-
error_page
-
ssl
-
stu_status內置狀態頁面
-
url重寫
-
if語法
-
定制日志
root path
root是指定網頁文件存放的路徑,可以在server配置段,也可以在location配置段, 存在location配置段時: 絕對路徑:優先級最高 root /vhosts/web/; location / { root /vhosts/web1; #訪問的是/vhosts/web1/index.html index index.html index.htm; } 相對路徑:是相對于nginx安裝路徑目錄下html root /vhosts/web/; location / { root html; #相對路徑,訪問的是/usr/local/nginx/html/index.html, index index.html index.htm; } 存在server配置段時:location建議不要配置. root /vhosts/web/; #此時才是訪問/vhosts/web/index.html location / { index index.html index.htm; }
location [ = | ~ | ~* | ^~ ] uri { … }
符號表示匹配后面uri的優先級,[=] > [^~] > [~|~*] >不帶任何修飾符,其中[~]表示區分大小寫,[~*]表示不區分大小寫 測試圖片,圖片本身就標注了存放位置:
測試目錄: [root@node2 vhosts]# tree . ├── a.png ├── web1 │ ├── documents │ │ └── a.png │ └── images │ └── a.png ├── web2 │ ├── documents │ │ └── a.png │ └── images │ └── a.png └── web3 ├── documents │ └── a.png └── images └── a.png 測試配置: location /documents/ { root /vhosts/web1; index index.html index.htm; } location ^~ /images/ { root /vhosts/web2; index index.html index.htm; } location ~* \.(png|jpg|jpeg)$ { root /vhosts/web3; index index.html index.htm; } location = /a.png { root /vhosts; index index.html index.htm; } 測試結果: 當我們訪問:192.168.3.20/images/a.png 說明:根據我們的URL[/images/a.png],服務器匹配location事先定義好的url,可以匹配到第二個與第三個location,因為[^~] > [~*],所在才有看到的是/vhosts/web2/images/a.png
當我們訪問:192.168.3.20/documents/a.png 說明:根據我們的URL[/documents/a.png],服務器匹配location事先定義好的url,可以匹配到第一個與第三個location,因為[~|~*] >不帶任何修飾符,所以才看到的是/vhosts/web3/documents/a.png
[=]的匹配也可以參考如上說明, 結論:匹配方式是根據用戶輸入的URL部分去匹配我們定義的location,當URL可以匹配到多個location的時候,就有了優先級的定義,
優先級:[=] > [^~] > [~|~] >不帶任何修飾符*
alias path
訪問資源是的URL與實際資源存儲位置不一樣,就需要別名定義實際存儲位置, location /documents/ { alias /vhosts/web1/; index index.html index.htm; } 當我們訪問192.168.3.20/documents/時,顯示的卻是:
error_page
根據http響應狀態碼來指明特定的錯誤頁面 location / { root /vhosts/web1/; index index.html index.htm; } error_page 404 /404.html; #當訪問不存在的資源時,就會回應404錯誤代碼,根據404錯誤代碼會顯示特定的404.html,這個頁面是存在與/vhosts/web1/目錄下
ssl
編譯時需要選擇此模塊 --with-http_ssl_module(httpd程序是需要安裝mod_ssl模塊,并且配置是在ssl配置文件中配置) CA服務器生成私鑰,自己對自己簽名 CA]# touch index.txt CA]# echo 01 > serial CA]# (umask 077; openssl genrsa -out private/cakey.pem 2048;) CA]# openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 3560 客戶端服務器生成私鑰,并且生成證書申請請求 ~]# mkdir /usr/local/nginx/ssl ~]# cd /usr/local/nginx/ssl ssl]# openssl genrsa -out node2.key 1024 ssl]# openssl req -new -key node2.key -out node2.csr -days 365 ssl]# scp node2.csr root@ns1:/tmp/node2.csr CA服務器對客戶端傳送過來的證書請求簽發 CA]# openssl ca -in /tmp/node2.csr -out ./certs/node2.stu.com.crt -days 365 CA]# scp certs/node2.stu.com.crt root@node2:/usr/local/nginx/ssl/ Nginx服務器配置支持ssl,我們在編譯此nginx服務器時就包括了 server { listen 443 ssl; server_name localhost; ssl_certificate /usr/local/nginx/ssl/node2.stu.com.crt; #從CA申請過來的證書 ssl_certificate_key /usr/local/nginx/ssl/node2.key; #私鑰 ssl_session_cache shared:SSL:1m; #ssl會話共享緩存,1分鐘時間 ssl_session_timeout 5m; #ssl會話超時時間5分鐘 ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; location / { root /vhosts/web1; index index.html index.htm; } }
stu_status內置狀態頁面
在location中定義: location /nginx-status { allow 192.168.3.0/24; #基于IP地址訪問控制 deny all; stub_status on; #開啟狀態頁面 access_log off; #關閉狀態頁面的訪問日志 } 測試訪問: [root@node3 nginx]# curl http://node2.stu.com/nginx-status Active connections: 2 server accepts handled requests 235 235 1239 Reading: 0 Writing: 1 Waiting: 1 =====解釋如下===== Active connections:當前所有處于打開狀態的連接數 accepts:已經接收進來的連接 handled:已經處理過的連接 requests:已經處理過的請求數 Reading:正處于接收請求狀態的連接數 Writing:請求已經接收完成,正處于處理請求或發送響應過程的連接數 Waiting:保持連接且處理于活動狀態的連接數
url重寫
將用戶請求特定資源的URL路徑修改重定向到其他路徑,(break,last,redirect,permanent是可選參數) location / { root /vhosts/web2; rewrite /documents/(.*\.png)$ /images/$1 break; #將訪問/vhosts/web2/documents/路徑下以.png結尾的資源,全部重定向到/vhosts/web2/images/ index index.html index.htm; }
break:一旦對此rewrute規則重寫后,由user agent對新的url重新發起新的請求,且不會在被location內的rewrite規則檢查, last:一旦對此rewrute規則重寫后,由user agent對新的url重新發起新的請求,如果還有被location內的rewrite匹配中那么就繼續重寫URL,這是多么痛的領悟, location / { root /vhosts/web2; rewrite /documents/(.*\.png)$ /images/$1 last; #采用last,重寫后的URL會匹配到下面一條location規則,并且還會被在重寫, index index.html index.htm; } location ^~ /images/ { #利用^~強行將URL的images抓到此location下進行URL重寫 root /vhosts/web1/; rewrite /images/(.*\.png)$ /documents/$1 break; #用戶輸入node2.stu.com/documents/a.png最后其實訪問的是/vhosts/web1/documents/a.png index index.html index.htm; }
redirect:以302狀態響應碼(臨時重定向),返回新的URL, location / { root /vhosts/web2; rewrite /documents/(.*\.png)$ /images/$1 redirect; index index.html index.htm; }
permanent:以301 #狀態響應碼(永久重定向),返回新的URL, location / { root /vhosts/web2; rewrite /documents/(.*\.png)$ /images/$1 permanent; index index.html index.htm; }
if語法
if語句的語法:if(condition) {},應用位置:server,location配置段 condition: 1. 變量賦值:禁止變量賦值為空或者以"0"開頭,其他都OK 2. 以變量為操作數構成的比較表達式,可使用=,!=類似操作符比較 3. 正則表達式的模式匹配操作 ~:區分大小寫 ~*:不區分大小寫 !~和!~*對上面兩種測試取反 4. 測試路徑為文件的可能性:-f | !-f 5. 測試指定路徑為目錄的可能性:-d | !-d 6. 測試文件存在性:-e | !-e 7. 檢查文件是否有執行權限:-x | !-x location / { root /vhosts/web2; if ($http_user_agent ~* Chrome) { #如果是谷歌瀏覽器就把URL重寫到/vhosts/web2/images/index.html rewrite ^(.*)$ /images/$1 break; } index index.html index.htm; }
Embedded Variables $http_user_agent $http_cookie $connection #connection serial number (1.3.8, 1.2.5) $connection_requests #current number of requests made through a connection (1.3.8, 1.2.5) $content_length #“Content-Length” request header field $content_type #“Content-Type” request header field $cookie_name #the name cookie $host #in this order of precedence: host name from the request line, or host name from the “Host” request header field, or the server name matching a request $hostname #host name $nginx_version #nginx version $pid #PID of the worker process $remote_addr #client address $remote_port #client port $remote_user #user name supplied with the Basic authentication $time_local #local time in the Common Log Format (1.3.12, 1.2.7) $server_name #name of the server which accepted a request $server_port #port of the server which accepted a request $server_protocol #request protocol, usually “HTTP/1.0”, “HTTP/1.1”, or “HTTP/2.0” look look www.nginx.org http://nginx.org/en/docs/http/ngx_http_core_module.html ======防盜鏈===== valid_referers none blocked server_names *.example.com example.* www.example.org/galleries/ ~\.google\.; if ($invalid_referer) { #invalid_referer表示該用戶是從哪個位置鏈接過來本網站的 return 403; }
定制日志
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; #上面定義的日志在此處調用,定義日志信息的變量都是內置變量
七.Nginx LNMP
PHP程序可以作為Apache的httpd模塊運行,也可以通過FPM的方式與httpd聯動. 而PHP程序與Nginx程序聯動只能以FPM的方式. 準備Mariadb: src]# tar xf mariadb-5.5.46-linux-x86_64.tar.gz src]# ln -sv /usr/src/mariadb-5.5.46-linux-x86_64 /usr/local/mysql src]# cd /usr/local/mysql mysql]# groupadd -r mysql mysql]# useradd -r -g mysql -s /sbin/nologin -M mysql mysql]# chown -R mysql:mysql . mysql]# mkdir -pv /mydata/data mysql]# chown -R mysql:mysql /mydata/data mysql]# scripts/mysql_install_db --user=mysql --datadir=/mydata/data mysql]# chown -R root . mysql]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld mysql]# chkconfig --add mysqld mysql]# chkconfig mysqld on mysql]# mkdir /etc/mysql mysql]# cp support-files/my-large.cnf /etc/mysql/my.conf mysql]# vi /etc/mysql/my.conf datadir=/mydata/data mysql]# echo "export PATH=/usr/local/mysql/bin:$PATH" > /etc/profile.d/mysql.sh #還有include文件,庫文件,man文件沒有做鏈接. mysql]# . /etc/profile.d/mysql.sh mysql]# service mysqld restart #遇到一個錯誤,PID file not found ,重啟之后就可以了,用ps -aux | grep mysqld 都看不到任何mysqld的進程 MySQL server PID file could not be found! [FAILED] Starting MySQL.. [FAILED] mysql]# service mysqld start Starting MySQL.. [ OK ] =====安裝php===== php-5.4.26]# cd /usr/src/php-5.4.26 php-5.4.26]# yum install pcre-devel openssl-devel libxml2-devel php-gd freetype-devel libjpeg-devel libpng-devel bzip2-devel libmcrypt-devel -y php-5.4.26]# ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --enable-sockets --enable-fpm --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 php-5.4.26]# make -j 4 && make install php-5.4.26]# cp php.ini-production /etc/php.ini php-5.4.26]# cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm php-5.4.26]# chmod +x /etc/rc.d/init.d/php-fpm php-5.4.26]# chkconfig --add php-fpm php-5.4.26]# chkconfig php-fpm on php-5.4.26]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf php-5.4.26]# vi /usr/local/php/etc/php-fpm.conf pm.max_children = 50 pm.start_servers = 5 pm.min_spare_servers = 2 #這個數值不能大于start_server,想想知道,不然php服務起不來 pm.max_spare_servers = 8 pid = /usr/local/php/var/run/php-fpm.pid php-5.4.26]# service php-fpm start ======配置nginx支持php===== location ~ \.php$ { root /vhosts/web4/; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /vhosts/web4$fastcgi_script_name; #需要指定我們指定存放php程序網站的路徑 include fastcgi_params; } 上傳phpmyadmin測試(怎么是這個鬼樣子,原來是瀏覽器的緣故谷歌不會):
(虛擬主機部分實驗忘記做了= =!)
OK,告一段落。
原創文章,作者:nice_neo_linux,如若轉載,請注明出處:http://www.www58058.com/19901