Nginx動靜分離javascript
實驗拓撲:php
實驗環境:css
Nginx分發器 192.168.42.175 xuegod175.cnhtml
Web1服務器 192.168.42.176 xuegod176.cn 做爲p_w_picpath圖片web服務器java
Web2服務器 192.168.42.177 xuegod177.cn 做爲php web服務器mysql
實驗注意:關閉iptables selinux 配置好hosts 文件,兩臺web服務器這裏均使用了nginxlinux
實驗配置:nginx
nginx實驗配置web
[root@xuegod175 ~]# cat /usr/local/nginx/conf/nginx.conf user nginx nginx; worker_processes 1; pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; #負責壓縮數據流 gzip on; gzip_min_length 1000; gzip_types text/plain text/css application/x-javascript; upstream web { server 192.168.42.175 weight=1 max_fails=2 fail_timeout=2; } upstream p_w_picpath { server 192.168.42.176 weight=1 max_fails=2 fail_timeout=2; } upstream php { server 192.168.42.177 weight=1 max_fails=2 fail_timeout=2; } server { listen 80; server_name 192.168.42.175; location / { root html/web; index index.php index.html; } location ~* \.php$ { root html; proxy_pass http://php; } location ~* ".(jpg|png|jpeg|gif)" { proxy_pass http://p_w_picpath; } } } Web1服務器配置 [root@xuegod176 conf]# cat nginx.conf worker_processes 1; user nginx nginx ; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } location ~ .*\.(gif|jpg|jpeg|png)$ { expires 24h; access_log /usr/local/nginx/logs/p_w_picpaths.log; 圖片日誌位置 proxy_store on; proxy_store_access user:rw group:rw all:rw; proxy_redirect off; proxy_set_header Host 127.0.0.1; client_max_body_size 10m; client_body_buffer_size 1280k; proxy_connect_timeout 900; proxy_send_timeout 900; proxy_read_timeout 900; proxy_buffer_size 40k; proxy_buffers 40 320k; proxy_busy_buffers_size 640k; proxy_temp_file_write_size 640k; } location ~ \.php$ { root html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; include fastcgi_params; } } }
配置測試文件(支持圖片)sql
[root@xuegod176 html]# ls
50x.html a abc.jpg bin binbin.jpeg index.html index.htmlbak nginx.conf
從客戶端進行訪問測試
說明nginx web1服務已經成功支持訪問圖片
經過分發器進行訪問測試(圖片文件是web1服務器的)
說明nginx讀寫分離已經成功 nginx分發器能夠正常的讀取web1服務器的圖片文件
Web2服務器配置
爲了使nginx可以支持訪問php環境,這裏特地安裝了lnmp環境
安裝mysql
[root@xuegod177 ]yum -y install mysql mysql-server mysql-devel
[root@xuegod177 ] /etc/init.d/mysqld start
安裝php
[root@xuegod177 ]yum install php php-mysql php-common php-gd php-mbstring php-mcrypt php-devel php-xml –y
[root@xuegod177 ]yum install php-fpm –y
[root@xuegod177 ]/etc/init.d/php-fpm start
[root@xuegod177 html]# ls
50x.html index.html index.html.bak index.php
[root@xuegod177 html]# cat index.php 建立測試文件
<?php phpinfo(); ?>
[root@xuegod177 conf]# cat nginx.conf user nginx nginx; worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } location ~ \.php$ { root html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; 使nginx 支持訪問php界面 fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; include fastcgi_params; } } }
Web2訪問測試:
說明177web 已經支持php 能夠正常使用訪問。
動靜分離訪問進行測試
訪問nginx分發器 index.php(這個文件是177web2服務器上的),nginx分發器能夠正常讀取172 web2服務器的php 文件,說明讀寫分離已經成功。