一、Nginx
是一個高性能的HTTP和反向代理服務器,同時也是一個IMAP/POP3/SMTP代理服務器;
在鏈接高併發的狀況下,優越性明顯要高於apache。
1.1認識
Apache,Nginx,Lighttpd(適用於非Java程序寫的頁面)
Tomcat,Jboss(適用於Java程序寫的)
用戶概念:源碼包安裝的時候須要創建一個用戶,該用戶用來執行該服務(若是沒有創建,系統默認使用nobody賬號),以防使用root形成不安全的後果,而yum源在裝包的時候會自動爲系統創建一個用戶。php
配置文件:/usr/local/nginx/conf/nginx.conf
日誌文件:/usr/local/nginx/logs
網頁頁面:/usr/local/nginx/html/index.html
自定義網頁頁面:/usr/local/nginx/目錄名稱css
模塊化安裝須要的模塊
./configure --with-模塊名稱 --with-模塊名稱html
1.2對原有的nginx添加模塊(升級)
一、刪除原有的解包後的nginx的目錄,從新解壓
二、cd到該目錄下
三、./configure --with-httpd_ssl_module
四、make (把源碼變成二進制程序,多了一個nginx執行程序)
五、不能make install (會覆蓋objs原有的文件)
六、cp objs/nginx /usr/local/nginx/sbin #升級只須要拷貝
1.3部署
1)使用源碼包安裝nginx軟件包
[root@svr5 ~]# yum –y install gcc pcre-devel openssl-devel //安裝常見依賴包 (gcc-c++)
[root@svr5 ~]# useradd –s /sbin/nologin nginx
[root@svr5 ~]# tar -xf nginx-1.8.0.tar.gz
[root@svr5 ~]# cd nginx-1.8.0
[root@svr5 nginx-1.8.0]# ./configure \
#> --prefix=/usr/local/nginx \ //指定安裝路徑
#> --user=nginx \ //指定用戶
#> --group=nginx \ //指定組
#> --with-http_ssl_module //開啓SSL加密功能
.. ..
make & make installpython
2)nginx命令的用法 (能夠創建軟鏈接,執行方便)
[root@svr5 ~]# /usr/local/nginx/sbin/nginx //啓動服務
[root@svr5 ~]# /usr/local/nginx/sbin/nginx -s stop //關閉服務
[root@svr5 ~]# /usr/local/nginx/sbin/nginx -s reload //從新加載配置文件
[root@svr5 ~]# /usr/local/nginx/sbin/nginx –V //查看軟件信息
ln -s /usr/local/nginx/sbin/nginx /usr/sbin/ (啓動服務只需輸入nginx)
[root@svr5 ~]# netstat -anptu | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0: LISTEN 10441/ngi
二、用戶認證
2.1修改Nginx配置文件
[root@pc205 ~]# vim /usr/local/nginx/conf/nginx.conf
server {
listen 80; #至關於 :80
server_name localhost;
auth_basic "Input Password:"; #認證提示符
auth_basic_user_file "/usr/local/nginx/pass"; #認證密碼文件(文件不存在)
location / {
root html;
index index.html index.htm;
}
}
一個server是一個網站
2.2生成密碼文件,建立用戶及密碼
使用htpasswd命令建立帳戶文件,須要確保系統中已經安裝了httpd-tools
[root@svr5 ~]# yum -y install httpd-tools
[root@svr5 ~]# htpasswd -cm /usr/local/nginx/pass tom //建立密碼文件(和配置文件內保持一致)
New password:
Re-type new password:
Adding password for user tom
[root@svr5 ~]# htpasswd -m /usr/local/nginx/pass jerry
//追加用戶,不使用-c選項
New password:
Re-type new password:
Adding password for user jerrymysql
[root@service ~]# cat /usr/local/nginx/pass
2.3重啓Nginx服務
[root@svr5 ~]# /usr/local/nginx/sbin/nginx –s reload nginx
tailf /usr/local/nginx/logs/ #動態查看錯誤信息c++
三、基於域名的虛擬主機
3.1服務端:
1)修改Nginx服務配置,添加相關虛擬主機配置以下
[root@svr5 ~]# vim /usr/local/nginx/conf/nginx.conf
server {
listen 80; //端口
server_name www.aa.com; //域名
auth_basic "Input Password:"; //認證提示符
auth_basic_user_file "/usr/local/nginx/pass"; //認證密碼文件
location / {
root html; //指定網站根路徑
index index.html index.htm;
}
}
server {
listen 80; //端口
server_name www.bb.com; //域名
location / {
root www; //指定網站根路徑(須要建立目錄名字爲www)
index index.html index.htm;
}
}
在文件中使用ctrl+v並移動上下鍵能夠選定字符,按x能夠刪除
2)建立網站根目錄及對應首頁文件
[root@svr5 ~]# mkdir /usr/local/nginx/www
[root@svr5 ~]# echo "www" > /usr/local/nginx/www/index.html
3)重啓nginx服務
[root@svr5 ~]# /usr/local/nginx/sbin/nginx –s reload
3.2客戶端:
1)修改客戶端主機192.168.4.100的/etc/hosts文件,進行域名解析
/etc/hosts #本地域名解析文件 在客戶端上改 優先級高於DNS
[root@client ~]# vim /etc/hosts
192.168.4.5 www.aa.com www.bb.com
2)測試 firefox http://www.aa.com firefox http://www.bb.com
四、SSL虛擬主機
加密算法:
(1)對稱加密
(2)非對稱加密
(3)哈希值 md5sum +文件
4.1生成私鑰與證書
[root@svr5 ~]# cd /usr/local/nginx/conf
[root@svr5 ~]# openssl genrsa -out cert.key (或者openssl genrsa > cert.key) //生成私鑰
[root@svr5 ~]# openssl req -new -x509 -key cert.key -out cert.pem //生成證書
國家,省份,城市,公司,部門,主機名
4.2修改Nginx配置文件,設置加密網站的虛擬主機
[root@svr5 ~]# vim /usr/local/nginx/conf/nginx.conf
server {
listen 443 ssl;
server_name www.cc.com;
ssl_certificate cert.pem; #證書名稱和生成的保持一致
ssl_certificate_key cert.key; #私鑰名稱和生成的保持一致
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
root html; #此處修改須要在/usr/local/nginx/下新建名稱一致的目錄名
index index.html index.htm;
}
}
ngnix -s reload #從新加載
4.3客戶端驗證
[root@client ~]# vim /etc/hosts
192.168.4.5 www.cc.com www.aa.com www.bb.com
[root@client ~]# firefox https://www.cc.com //信任證書後能夠訪問
五、Nginx反向代理
負載均衡;檢查後臺狀況(web高可用);
5.1部署後端Web服務器
yum -y install httpd
echo "192.168.2.100" > /var/www/html/index.html
systemctl restart httpd
5.2部署Nginx服務器
因爲配置文件在使用過程當中,在/usr/local/nginx/conf下存在自帶配置備份文件
nginx.conf.default
在實際工做中修改配置文件前對其進行先備份
cp nginx.conf.default nginx.conf (對於本實驗覆蓋後從新作)
修改/usr/local/nginx/conf/nginx.conf配置文件
http {
upstream webserver { #定義一個web集羣,取名webserver
server 192.168.2.100:80; #後臺服務器
server 192.168.2.200:80;
} #該函數定義集羣
#能夠添加weight權重:調用次數 ,
#max_fails失敗次數:容許最多鏈接後臺web失敗的次數
#fail_timeout=10 超時時間:當失敗後,10s後再詢問後臺的web是否正常(加down10s後不在詢問)
server {
listen 80;
server_name www.tarena.com;
location / { #匹配用戶的地址欄
proxy_pass http://webserver; #調用集羣
root html; #該路徑再也不尋找
5.3重啓服務
/usr/local/nginx/sbin/nginx –s reload
客戶端輪詢訪問2個webweb
5.4設置相同客戶端訪問相同Web服務器
upstream webserver { #定義一個web集羣,取名webserver
ip_hash; #客戶端第一次訪問以後,之後繼續分配到這臺web
server 192.168.2.100:80; #後臺服務器
server 192.168.2.200:80;
}
客戶端訪問只訪問一個web 算法
一、動態網站
LNMP(Linux,nginx,mariadb,php,python)
1.1安裝nginx
1)解包 tar -xf
配置 ./configure --prefix=/usr/local/nginx --with-http_ssl_module
編譯 make
安裝 make install
2)mariadb 使用mysql命令
mariadb-server 存放數據的地方 監聽3306端口
mariadb-devel 依賴關係
3)php【解釋器】
<?php #開頭 內容 結尾 ?>
php-fpm 監聽9000端口 自動解釋代碼的服務
php-mysql 擴展包,鏈接數據庫
4)啓動服務以及查看端口狀態
/usr/local/nginx/sbin/nginx #啓動Nginx服務
netstat -utnlp | grep :80
systemctl start mariadb #啓動數據庫服務
netstat -utnlp | grep :3306 或者 systemctl status mariadb
systemctl start php-fpm #啓動php-fpm服務
netstat -utnlp | grep :9000 或者 systemctl status php-fpmsql
1.2動靜分離
當用戶訪問網站時會根據location來匹配尋找的頁面,沒有匹配的就匹配 / 中的內容
須要將php腳本放在/usr/local/nginx/html下,並修改下面的配置文件
vim /usr/local/nginx/conf/nginx.conf
location ~ .php$ { #匹配是否以.php結尾
root html; #頁面的位置
fastcgi_pass 127.0.0.1:9000; #把找到的頁面給了9000(php-fpm IP與端口)
fastcgi_index index.php;
include fastcgi.conf; #加載fastcgi.conf參數文件
}
日誌查看:tailf /usr/local/nginx/logs/error.log
ls /var/log/php-fpm/error.log
測試:firefox http://192.168.4.5/1.php
vim 1.php
<?php
phpinfo();
?>
1.3 構建LNMP
FastCGI :是一種常駐(long-live)型的CGI
將CGI解釋器進程保持在內存中,進行維護與調度
配置文件路徑:/etc/php-fpm.d/www.conf
[root@svr5 etc]# vim /etc/php-fpm.d/www.conf #無須修改
(3)建立PHP測試頁面,鏈接MariaDB數據庫:
[root@svr5 ~]# vim /usr/local/nginx/html/test2.php
<?php
$links=mysql_connect("localhost","root","密碼");
//注意:root爲mysql帳戶名稱,密碼須要修改成實際mysql密碼,無密碼則留空便可
if($links){
echo "link db ok!!!";
}
else{
echo "link db no!!!";
}
?>
(4)建立PHP測試頁面,鏈接並查詢MariaDB數據庫
[root@svr5 ~]# vim /usr/local/nginx/html/test3.php
<?php
$mysqli = new mysqli('localhost','root','','mysql');
if (mysqli_connect_errno()){
die('Unable to connect!'). mysqli_connect_error();
}
$sql = "select * from user";
$result = $mysqli->query($sql);
while($row = $result->fetch_array()){
printf("Host:%s",$row[0]);
printf("</br>");
printf("Name:%s",$row[1]);
printf("</br>");
}
?>
(5)客戶端使用瀏覽器訪問服務器PHP首頁文檔,檢驗是否成功
[root@client ~]# firefox http://192.168.4.5/test2.php
[root@client ~]# firefox http://192.168.4.5/test3.php
二、地址重寫
得到一個來訪的URL請求,而後改寫成服務器能夠處理的另外一個URL的過程
rewrite 舊連接(支持正則) 新連接 [選項];
選項:last 再也不讀其餘rewrite;break 再也不讀其餘語句,結束;
redirect 讓地址欄變化,用戶能看到url的改變(臨時);permament讓地址欄變化(永久)
1)修改配置文件(訪問a.html重定向到b.html)
[root@svr5 ~]# vim /usr/local/nginx/conf/nginx.conf
.. ..
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
rewrite /a.html /b.html; #訪問a.html的內容會跳到b.html (能夠縮短URL)
}
}
echo www.a.com > html/a.html
echo www.b.com > html/b.html
[root@svr5 ~]# /usr/local/nginx/sbin/nginx -s reload
[root@client ~]# firefox http://192.168.4.5/a.html
2)修改配置文件(訪問192.168.4.5的請求重定向至www.a.com)
[root@svr5 ~]# vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name localhost;
rewrite ^/ http://www.a.com/; #在進入網站以前匹配到以 / 開頭的都跳轉到該網站
location / {
root html;
index index.html index.htm;
}
}
[root@svr5 ~]# /usr/local/nginx/sbin/nginx -s reload
3)修改配置文件(訪問192.168.4.5/下面子頁面,重定向至www.tmooc.cn/下相同的頁面)
[root@svr5 ~]# vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name localhost;
rewrite ^/(.) http://www.a.com/$1; #訪問子網站都跳轉到現有對應的,一個()對應一個$
location / {
root html;
index index.html index.htm;
}
}
[root@svr5 ~]# /usr/local/nginx/sbin/nginx -s reload
訪問http://192.168.4.5/web/login_new.html 跳轉到
http://www.tmooc.cn/web/login_new.html
4)實現curl和火狐訪問相同鏈接返回的頁面不一樣
.. ..
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
if ($http_user_agent ~ url) { //識別客戶端curl瀏覽器 不分大小寫
rewrite ^(.)$ /curl/$1 break;
}
}
[root@svr5 ~]# echo "firefox" > /usr/local/nginx/html/test.html
[root@svr5 ~]# mkdir -p /usr/local/nginx/html/curl/
[root@svr5 ~]# echo "curl" > /usr/local/nginx/html/curl/test.html
[root@svr5 ~]# /usr/local/nginx/sbin/nginx -s reload
[root@client ~]# firefox http://192.168.4.5/test.html #出現firefox頁面
[root@client ~]# curl http://192.168.4.5/test.html #返回curl的信息
實際應用:區分是PC頁面或者手機頁面或者其餘
$http_user_agent 用戶請求的包含用戶的信息的變量
tailf /usr/local/nginx/logs/access.log #訪問信息日誌
192.168.4.254 - - [07/Jan/2018:21:48:16 -0500] "GET /test.html HTTP/1.1" 304 0 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Firefox/38.0"
192.168.4.254 - - [07/Jan/2018:21:48:16 -0500] "GET /favicon.ico HTTP/1.1" 404 168 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Firefox/38.0"
訪問服務器的用戶IP - 用戶名 時間 訪問什麼 用什麼系統訪問 用什麼瀏覽器訪問
[root@Proxy ~]# curl -A firefox http://192.168.4.5/test.html
Firefox #並不顯示curl
#curl假裝成firefox進行對服務器訪問,服務器訪問日誌顯示假裝後的信息
一、Nginx常見問題處理
1.1不顯示Nginx軟件版本號
server_tokens off/on; #服務器版本號信息
1.2併發量
ab –n 2000 –c 2000 http://192.168.4.5/ -c 併發量 -n 請求數
如何增大併發量的容量
1)經過nginx配置文件修改
worker_processes 2; #與CPU核心數量一致
events {
worker_connections 65535; #每一個worker最大併發鏈接數
use epoll;
}
2)經過Linux系統內核
ulimit -a #查看最大全部限制
ulimit –Hn 100000 #臨時有效
ulimit –Sn 100000 #臨時有效
-S軟限制 (用戶能夠修改) n(最大文件數量)
-H硬限制 (用戶不能夠修改) n(最大文件數量)
ss -anptu | grep nginx #實時查看併發量有多少人訪問 (| wc -l)
永久設置:
[root@svr5 ~]# vim /etc/security/limits.conf
<domain> <type> <item> <value>
/usr/local/nginx/conf/sbin/nginx -s reload