前端:nginx(fastcgi反代),IP地址192.168.206.11;
后端:httpd、PHP-fpm,接收php动态资源解释,IP地址192.168.206.22;
后端:nginx(web),接收静态资源访问,IP地址为192.168.206.33;
架构图:
ngx_proxy_module |---------------------- nginx | (IP:192.168.206.33) | 前端nginx(反向代理) ---------->| (IP:192.168.206.11) | | ngx_fastcgi_module |---------------------- php-fpm、mariadb (IP:192.168.206.22)
1、前端nginx反代配置(192.168.206.11):
=======================================
nginx反代主配置文件:
—————————-
user nginx; worker_processes 1; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; #启用http反向代理,缓存配置; proxy_cache_path /data/nginx/cache levels=1:1:1 keys_zone=pcache:10m max_size=2g inactive=10m; proxy_connect_timeout 30s; proxy_read_timeout 30s; proxy_send_timeout 30s; proxy_headers_hash_max_size 5120; proxy_headers_hash_bucket_size 640; #启用fastcgi反向代理,缓存配置; fastcgi_cache_path /data/nginx/fastcgicache levels=1:2:1 keys_zone=fcgicache:10m max_size=2g inactive=10m; sendfile on; #tcp_nopush on; keepalive_timeout 65; gzip on; gzip_comp_level 1; gzip_min_length 100k; gzip_buffers 16 8k; gzip_proxied off; #gzip_proxied no-cache no-store private; gzip_types text/css text/xml application/javascript; server_tokens off; include /etc/nginx/conf.d/*.conf; }
vhost配置:
————————
server { listen 8011; server_name abc.node1.com; root /data/nginxweb; index index.html index.php; location / { proxy_pass http://192.168.206.33:80; proxy_cache pcache; proxy_cache_key $request_uri; proxy_cache_methods GET HEAD; proxy_cache_min_uses 2; proxy_cache_valid 200 302 10m; proxy_cache_valid 301 1h; proxy_cache_use_stale http_502; } location ~* \.php$ { fastcgi_pass 192.168.206.22:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /data/apps/$fastcgi_script_name; include fastcgi_params; fastcgi_keep_conn on; fastcgi_cache fcgicache; fastcgi_cache_key $request_uri; fastcgi_cache_methods GET HEAD; fastcgi_cache_min_uses 2; fastcgi_cache_valid 200 302 10m; fastcgi_cache_valid 301 1h; fastcgi_cache_use_stale http_503; } }
2、后端php-fpm配置(192.168.206.22):
======================================
vim /etc/php-fpm.d/www.conf
——————————–
user = nginx group = nginx listen = 0.0.0.0:9000 listen.allowed_clients = 192.168.206.11
创建PHP会话缓存目录;
————————
mkdir /var/lib/php/session chown -R nginx.nginx /var/lib/php/session
解压PHPmyadmin到/data/apps目录下面,并创建软连接;
————————————————-
[root@node2 apps]# pwd /data/apps [root@node2 apps]# [root@node2 apps]# ll total 12 -rw-r--r-- 1 root root 62 Jun 20 13:12 index.php drwxr-xr-x 12 root root 4096 Jun 20 12:23 phpMyAdmin-4.8.1-all-languages lrwxrwxrwx 1 root root 30 Jun 20 13:23 pma -> phpMyAdmin-4.8.1-all-languages [root@node2 apps]#
进入pma目录,做如下配置:
———————————
cp config.sample.inc.php config.inc.php vim config.inc.php #添加随机字符串'ouewj09jdJLKDF'; $cfg['blowfish_secret'] = 'ouewj09jdJLKDF'; /* YOU MUST FILL IN THIS FOR COOKIE AUTH! */
3、后端nginx(web),192.168.206.33配置;
=====================================
解压PHPmyadmin到/data/apps目录下面,并创建软连接;
————————————————-
[root@node0 webroot]# pwd /data/webroot [root@node0 webroot]# ll total 10320 -rw-r--r-- 1 root root 26 Jun 19 20:47 index.html drwxr-xr-x 12 root root 4096 May 24 22:46 phpMyAdmin-4.8.1-all-languages -rw-r--r-- 1 root root 10552094 Jun 20 13:46 phpMyAdmin-4.8.1-all-languages.zip lrwxrwxrwx 1 root root 30 Jun 20 13:47 pma -> phpMyAdmin-4.8.1-all-languages [root@node0 webroot]#
vhost配置:
—————————
server { listen 80; server_name 192.168.206.33; #定义root、index; location / { root /data/webroot; index index.html index.htm; } #禁止IP地址访问,只允许通过制定域名访问web; #if ($host != 'lucifer.kouyuushinn.cn') { # rewrite /.* http://lucifer.kouyuushinn.cn permanent; # rewrite_log off; # } #图片类防盗链; #location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip|doc|pdf|gz|bz2|jpeg|bmp|xls)$ { # expires 7d; # valid_referers none blocked server_names *.kouyuushinn.cn; # if ($invalid_referer) { # return 403; # #rewrite ^/ http://lucifer.kouyuushinn.cn; #} #} #字符集设置; #charset koi8-r; #访问日志、错误日志; access_log /var/log/nginx/access.log main; error_log /var/log/nginx/error.log warn; #错误页; error_page 404 /404.html; error_page 500 502 503 504 /50x.html; location = /50x.html { root /data/webroot; } }
4、测试1,浏览器输入,http://192.168.206.11:8011/pma/index.php;
5、测试2,查看是否生成动态请求资源的缓存:
[root@node1 nginxweb]# ll /data/nginx/fastcgicache/ total 0 drwx------ 3 nginx nginx 16 Jun 20 14:30 1 [root@node1 nginxweb]# ll /data/nginx/fastcgicache/1/af/5/e251273eb74a8ee3f661a7af00915af1