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 文件,说明读写分离已经成功。