安裝Nginx的依賴:html
yum -y install pcre-devel zlib-devel openssl-develnginx
安裝源碼包Nginx的關聯:web
要先建立管理Nginx的系統用戶apache
useradd -M -s /sbin/nologin nginxvim
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module緩存
*************************************************************************************************
bash
1、Nginx反向代理服務器
1.配置環境一臺Nginx,一臺測試服務器,web1
負載均衡
[root@web1 ~]# yum install -y httpd
2.啓動httpd
curl
[root@web1 ~]# service httpd start 正在啓動 httpd: [肯定]
3.在httpd頁面寫好頁面
[root@web1 ~]# vim /var/www/html/index.html iiiiiiiiiiiiiiiiiiiiii
4.配置Nginx反向代理
vim /usr/local/nginx/conf/nginx.conf location / { proxy_pass http://192.168.18.201; }
5.頁面訪問Nginx的IP,會顯示httpd配置的頁面
2、Nginx負載均衡
一臺Nginx,兩臺web服務器
1.配置nginx負載均衡
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf upstream webservers { server 192.168.18.201 weight=1; #實驗環境用權重 server 192.168.18.202 weight=1; } server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { proxy_pass http://webservers; proxy_set_header X-Real-IP $remote_addr; } }
注,upstream是定義在server{ }以外的,不能定義在server{ }內部。定義好upstream以後,用proxy_pass引用一下便可。
2.從新加載一下配置文件
[root@nginx ~]# pkill ngixn [root@nginx ~]# /usr/local/nginx/sbin/nginx
3.頁面測試
注:不斷刷新就會發現web1與web2是交替出現的,達到了負載均衡的效果。
3、Nginx頁面緩存
proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=one:10m inactive=1m max_size=30g;
inactive=1m 若是緩存1分鐘沒人訪問,nginx 會刪除掉這些緩存 硬盤中的最大空間爲 30G;
1.配置一個簡單的Nginx緩存服務器
[root@nginx ~]# vim /etc/nginx/nginx.conf proxy_cache_path /data/nginx/cache/webserver levels=1:2 keys_zone=webserver:20m max_size=1g; upstream webservers { server 192.168.115.87:8080 weight=1 max_fails=2 fail_timeout=2; } server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { proxy_pass http://webservers; proxy_set_header X-Real-IP $remote_addr; proxy_cache webserver; proxy_cache_valid 200 10m; } }
2.創建緩存目錄
[root@nginx ~]# mkdir -pv /data/nginx/cache/webserver
注:建立的目錄要與配置文件裏寫的路徑同樣
3.重啓Nginx
[root@nginx ~]# pkill ngixn [root@nginx ~]# /usr/local/nginx/sbin/nginx
4.頁面刷新,而後停掉httpd服務器在刷新會發現頁面還會存在,而後去web服務器上查看緩存文件
[root@web1 63]# pwd /data/nginx/cache/webserver/f/63 [root@C0S1 63]# ls 681ad4c77694b65d61c9985553a2763f #緩存文件
4、Nginx讀寫分離
1修改配置文件
[root@nginx nginx]# vim /usr/local/nginx/conf/nginx.conf server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { proxy_pass http://192.168.18.202; if ($request_method = "PUT"){ proxy_pass http://192.168.18.201; } } }
2.重啓Nginx
[root@nginx ~]# pkill ngixn [root@nginx ~]# /usr/local/nginx/sbin/nginx
3.配置httpd的WebDAV功能
注,在<Directory "/var/www/html">下啓用就行。
4.從新啓動一下httpd
[root@web1 ~]# service httpd restart 中止 httpd: [肯定] 正在啓動 httpd: [肯定]
5.測試一下
[root@nginx ~]# curl http://192.168.18.201 <h1>web1.test.com</h1> [root@nginx ~]# curl http://192.168.18.202 <h1>web2.test.com</h1>
注,web1與web2訪問都沒問題。
[root@nginx ~]# curl -T /etc/issue http://192.168.18.202 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html><head> <title>405 Method Not Allowed</title> </head><body> <h1>Method Not Allowed</h1> The requested method PUT is not allowed for the URL /issue. <hr> <address>Apache/2.2.15 (CentOS) Server at 192.168.18.202 Port 80</address> </body></html>
注,咱們上傳文件到,web2上時,由於web2只人讀功能,因此沒有開戶WebDAV功能,因此顯示是405 Method Not Allowed。
[root@web1 ~]# setfacl -m u:apache:rwx /var/www/html/
下面咱們再來測試一下
[root@nginx ~]# curl -T /etc/issue http://192.168.18.201 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html><head> <title>201 Created</title> </head><body> <h1>Created</h1> Resource /issue has been created. <hr /> <address>Apache/2.2.15 (CentOS) Server at 192.168.18.201 Port 80</address> </body></html>
注,你們能夠看到咱們成功的上傳了文件,說明nginx讀寫分離功能配置完成。最後,咱們來查看一下上傳的文件。
[root@web1 ~]# cd /var/www/html/ [root@web1 html]# ll 總用量 12 drwxr-xr-x 2 root root 4096 9月 4 13:16 forum -rw-r--r-- 1 root root 23 9月 3 23:37 index.html -rw-r--r-- 1 apache apache 47 9月 4 14:06 issue
以上就是Nginx的方向代理、負載均衡、頁面緩存、讀寫分離。但願你們有所收穫。