nginx實現代理服務器功能1:
#環境: 172.16.253.223 #CentOS7.3,安裝nginx作為代理服務器 172.16.253.224 #CentOS7.3,安裝httpd作為服務器 172.16.253.188 #CentOS6.8,咱莊httpd作為圖片服務器 #223主機: yum install nginx vim /etc/nginx/conf.d/test.conf #注意必須以conf結尾 server { listen 80; server_name jing.li.io; location / { proxy_pass http://172.16.253.224/; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #告訴后端主機這個請求的真正地址是$remote_addr(客戶端真實的地址) } location ~* \.(jpg|png|jpeg)$ { #用正則之后 proxy_pass http://172.16.253.188; #這里不可以跟多余的uri } } systemctl start nginx vim /etc/hosts 172.16.253.223 jing.li.io #添加域名解析,物理機上的hosts也需要添加 #224主機 yum install httpd vim /etc/httpd/conf/httpd.conf #LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined #驗證這個后端服務器收到的請求報文的源IP是否被更改 vim /var/www/html/index.html <h1>OK</h1> systemctl start httpd tail /var/log/httpd/access_log - - - [23/Jun/2017:04:13:30 +0800] "GET /favicon.ico HTTP/1.0" 404 209 "http://jing.li.io/" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3095.5 Safari/537.36" #改前 172.16.253.188 - - [23/Jun/2017:04:19:04 +0800] "GET / HTTP/1.0" 304 - "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3095.5 Safari/537.36" #改后 #188主機 yum install httpd find /usr/share/ -name "*.jpg" -exec cp {} /var/www/html/ \; service httpd start #注意,都需要關閉防火墻和SELinux systemctl stop firewalld #C7 service iptables stop #C6 setenforce 0 #關閉SELinux #在瀏覽器上訪問jing.li.io以及jing.li.io/*.jpg #*為復制過去的某個圖片文件名稱
啟用緩存功能:
vim /etc/nginx/nginx.conf proxy_cache_path /data/nginx/cache levels=1:1:1 #注意,要放入http代碼段;下面代碼與這一行是相連續的,為了視圖方便,特意分開 #cache目錄需要建立出來 #定義3級子目錄,每一級只有16個 keys_zone=pcache:10m #定義鍵區域為pcache,可以定義多個,但不能重名,在內存中能使用10m max_size=2g; #/data/nginx/cache這個最大使用2g磁盤大小 proxy_cache_path /data/nginx/cache levels=1:1:1 keys_zone=pcache:10m max_size=2g; #這個為整段的代碼段 vim /etc/nginx/conf.d/test.conf #在location中添加為單獨location資源使用緩存,在server中加,表示所有資源都使用這個緩存 proxy_cache pacache; #調用這個緩存 proxy_cache_key $request_uri; #當多個域名指向同一站點,這種更加實用,這里必須添加 proxy_cache_valid 200 302 301 1h; #200,302,301響應碼可以在1小時內進行 proxy_cache_valid 404 1m; #404最好比較短
構建lnmp環境
#前端nginx主機處理靜態,后端fpm server處理動態。前端nginx也要有phpMyadmin #A機器配置為fpm server yum install php-fpm php-mysql php-mbstring php-mcrypt mariadb-server -y lftp 172.16.0.1/pub cd Sources/7.x86_64/php mget phpMyAdmin-4.xxxxx #將A機器網絡配置為內網主機 ifconfig eth0 192.168.10.11/24 up #啟動fpm server cd /etc/php-fpm.d/ vim www.conf listen = 0.0.0.0:9000 #listen.allowed_clients user = nginx #需要創建,會自動創建 group = nginx pm.max_chidren = 150 #最大進程數 pm_status_path status #狀態頁啟用,pong,ping也啟用 php_value[session.save_path] = /var/lib/php/session #可能需要創建 chown nginx:nginx /var/lib/php/session #與php-fpm進程運行者身份保持一致 systemctl start php-fpm.service #配置B機器作為nginx為反代服務器 vim /etc/nginx/conf.d/test.conf #原來的test文件可以刪掉 server { listen 80; server_name li.jing.io; index index.php index.html; #指明主頁 location / { root /data/nginx/html; #目錄需要創建 } location ~* \.php$ { fastcgi_pass 192.168.10.11:9000; fastcgi_index index.php; include fastcgi_params; #需要包含進來一起生效 fastcgi_param SCRIPT_FILENAME /data/apps/$fastcgi_script_name; #順序 } #vim /etc/nginx/fastcgi.pass #一般不改這個文件 #fastcgi_param SCRIPT_FILENAME /data/apps/$fastcgi_script_name; #表示,反代時需要把用戶請求的URL發送到服務器,路徑還必須指明為服務器文件系統路徑上的位置。 vim /data/nginx/index.html <h1>Nginx Server</h1> nginx -t nginx -s reload #A機器上創建主頁 mkdir /data/apps -pv vim /data/apps/index.php <?php phpinfo(); ?> #請求li.jing.io/index.php #請求li.jing.io #hosts文件記得更改 ##A機器配置phpMyadmin vim /etc/my.cnf.d/server.cnf skip_name_resolve=ON innodb_file_per_table=ON systemctl start mariadb.service ss -tnl #3306端口 mysql_secur_installation #需要確保root用戶有密碼 musql -uroot -plijing #lijing自己指定 #部署應用 tar xf phpMyadmin-xxxxx.tar.gz -C /data/apps/ cd /data/apps ln -sv phpxxxxxxx pma cd pma/ cp config.sample.inc.php config.inc.php vim config.inc.php $cfg....._secret'] = 'adada8b7c6d'; #添加隨機數 #注意,訪問的時候不能以pma下,他不會往后端反代 #再訪問li.jing.io/index.php,會發現圖片無法反代 scp php.....tar.gz 192.168.10.11:/root/ #切換到B機器 tar xf phpM......tar.gz -C /data/nginx/html cd /data/nginx/html ln -sv phpM..... pma #再次訪問li.jing.io/index.php
再配置一臺nginx
C機器 ifconfig eth0 192.16.10.12/24 up mkdir /data/nginx/html -pv #切換到B scp php.....tar.gz 192.168.10.11:/root/ #切換回C機器 tar xf phpM......tar.gz -C /data/nginx/html cd /data/nginx/html ln -sv phpM..... pma vim /etc/nginx/nginx.conf server { root /data/nginx/html; #添加 } systemctl start nginx #先關了httpd #切換回B機器(Nginx) vim /etc/nginx/conf.d/test.conf location / { proxy_pass http://192.168.10.12:80; #添加 } nginx -s reload #訪問li.jing.io #查看192.168.10.12,C機器的日志 tail /var/log/nigx/access.log #做壓測: yum install httpd-tools ab -c 100 -n 5000 http://li.jing.io/pma/index.php #hosts文件需要添加解析
fastcgi添加緩存
#B機器 vim /etc/nginx/nginx.conf http { fastcgi_cache_path /data/nginx/fcgicache levels=2:2:2 keys_zone=fcache:10m max_size=2g; } vim /etc/nginx/conf.d/test.conf location ~* \.php$ { fastcgi_cache fcache; #調用上面定義的fcache fastcgi_cache_key $request_uri; #定義鍵位$requesst_uri fastcgi_cache_valid 200 302 10m; fastcgi_cache_valid 301 1h; fastcgi_cache_valid any 1m; #添加這些 } #先請求得到緩存之后再去壓測 ab -c 100 -n 2000 http://li.jing.io/index.php #緩存 !ab
添加測試頁
#B vim /etc/nginx/conf.d/test.conf location ~*/(status|ping)$ { include fastcgi_params; fastcgi_pass 192.168.10.11:9000; fastcgi_param SCRIPT_FILENAME $fastcig_script_name; } #訪問li.jing.io/status #li.jing.io/ping #li.jinng.io/status?full #把fpm server換成ap
原創文章,作者:半斤八兩,如若轉載,請注明出處:http://www.www58058.com/78711