nginx1.15反向代理:
正向代理:
用户用浏览器与代理服务器建立连接。 代理服务器去找web服务器。
代理服务器是在客户端浏览器设置。
反向代理:
用户与反向代理服务器建立连接。反向代理服务器将请求代理到后端web服务器。
反向代理服务器是在服务器端配置代理。常用的反向代理为:nginx.
系统环境: centos7.5
安装环境:
服务名称 | 服务IP地址 | 服务描述 |
node01 | 18.18.23.109 | nginx1.15 |
node02 | 18.18.23.110 |
mysql5.7.22 |
node03 | 18.18.23.111 | tomcat8/php7.1.2 |
一. nginx反向代理tomcat:
1.nginx反向代理tomcat配置:
nginx.conf配置:
http段配置: include vhost/*.conf;
#mkdir vhosts
#vi vhosts/proxy-tomcat.conf
server { listen 88; server_name tomcat; access_log logs/tomcat-access.log; error_log logs/tomcat-error.log; location / { proxy_pass http://18.18.23.111:8080;
tomcat服务器配置:
安装jdk1.8
配置环境变量:
export JAVA_HOME=/usr/local/jdk1.8.0_45 export PATH=$JAVA_HOME/bin:$PATH export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
安装tomcat服务器:
下载tomcat:
#curl -o apache-tomcat-8.5.31.tar.gz https://tomcat.apache.org/download-80.cgi#8.5.31/apache-tomcat-8.5.31.tar.gz
解压tomcat.
启动tomcat.
./bin/start.sh
浏览器访问:
http://18.18.23.109:88 , 返回的是tomcat的页面。
二、nginx1.15反向代理php
1.安装php依赖的第三方库:
# yum -y install libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel pcre-devel gd-devel libcurl-devel curl-devel libxslt-devel openssl openssl-devel
2.编译安装php:
# tar -zxvf php-7.1.2.tar.gz #cd php-7.1.2 # ./configure --prefix=/usr/local/php7 --with-config-file-path=/usr/local/php/etc --with-mysqli --enable-mysqlnd --enable-embedded-mysqli --with-curl --with-freetype-dir --with-gd --with-gettext --with-iconv-dir --with-kerberos --with-libdir=lib64 --with-libxml-dir --with-mysqli --with-openssl --with-pcre-regex --with-pdo-mysql --with-pear --with-png-dir --with-xmlrpc --with-xsl --with-zlib --enable-fpm --enable-bcmath --enable-libxml --enable-inline-optimization --enable-gd-native-ttf --enable-mbregex --enable-mbstring --enable-opcache --enable-pcntl --enable-shmop --enable-soap --enable-sockets --enable-sysvsem --enable-xml --enable-zip --with-jpeg-dir --with-png-dir --with-iconv --with-pcre-dir --with-mhash #make && make install
3. 复制并修改配置文件:
在PHP服务器上,建立nginx用户,要保证和nginx服务器上的nginx用户id 号、组id号一致。
# cp php.ini-development /usr/local/php7/etc/php.ini
配置php-fpm
# cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf # cp /usr/local/php7/etc/php-fpm.d/www.conf.default /usr/local/php7/etc/php-fpm.d/www.conf
修改配置文件:
# vim /usr/local/php7/etc/php.ini
short_open_tag = On default_charset = "UTF-8" date.timezone = Asia/Shanghai
#vim /usr/local/php7/etc/php-fpm.d/www.conf
[www] user = nginx group = nginx listen.owner = nginx listen.group = nginx listen = 18.18.23.111:9000 pm = dynamic pm.max_children = 50 #子进程最大数量 pm.start_servers = 20 #在启动时创建的子进程数量 pm.min_spare_servers = 5 # 空闲子进程的最小数量 pm.max_spare_servers = 50 # 空闲子进程的最大数量 pm.process_idle_timeout = 10s # 一个空闲进程空闲多少秒后被杀死 php_admin_value[error_log] = /var/log/fpm-php.www.log
4. 启动php-fpm:
添加为系统服务:
# cat /usr/lib/systemd/system/php-fpm.service [Unit] Description=php7.1.2 After=network.target [Service] Type=forking ExecStart=/usr/local/php7/sbin/php-fpm ExecStop=/bin/pkill -9 php-fpm PrivateTmp=true [Install] WantedBy=multi-user.target
#systemctl daemon-reload #systemctl enable php-fpm #systemctl start php-fpm
5.Nginx 配置php程序通过FastCGI转发给php
# vim /usr/local/nginx/conf/nginx.conf
在server段上面配置:
include vhost/*.conf;
# vim /usr/local/nginx/conf/vhost/proxyphp.conf
server { listen 81; server_name php; access_log logs/php-access.log; location / { root html/wordpress; index index.php index.html index.htm; } location ~ \.php$ { root html/wordpress; fastcgi_pass 18.18.23.111:9000; #php服务器ip地址。 fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; include fastcgi.conf; } }
php脚本存放位置:
# tar -zxvf wordpress-4.9.4-zh_CN.tar.gz -C /usr/local/nginx/html/ #chown -R nginx /usr/local/nginx/wordpress
6.mysql配置:
mysql> create database wordpress; mysql> create user 'blog'@'%' identified by '123'; mysql> grant all privileges on wordpress.* to 'blog'@'%'; mysql> flush privileges;
浏览器访问:
http://18.18.23.109:81/index.php
wordpress已经搭建完成。
原文地址:http://blog.51cto.com/liyuanjie/2153823