nginx获取远程资源(静态及动态)

nginx获取远程动态及静态资源

本机地址:192.168.43.128

远程主机地址:192.168.43.129

获取动态资源(以php文件为例)

  • 远程端
新建一个php文件,记录其路径
[root@windfantasy ~]# cd /usr/local/apache/htdocs/wml.com/
[root@windfantasy wml.com]# ls
index.php
[root@windfantasy wml.com]# cat index.php 
<?php
   phpinfo();
?>
[root@windfantasy wml.com]# pwd
/usr/local/apache/htdocs/wml.com

关闭防火墙和selinux
[root@windfantasy wml.com]# systemctl stop firewalld
[root@windfantasy wml.com]# setenforce 0
设置php-fpm服务监听所有主机的9000端口
[root@windfantasy wml.com]# vim /usr/local/php7/etc/php-fpm.d/www.conf
listen = 0.0.0.0:9000

启用php-fpm服务
[root@windfantasy wml.com]# service php-fpm start
Starting php-fpm  done

本地

创建nginx用户和组
[root@windfantasy ~]# useradd -r -M -s /sbin/nologin nginx

创建以下目录,并将其属主和属组为nginx
[root@windfantasy ~]# mkdir -p /var/log/nginx
[root@windfantasy ~]# chown -R nginx.nginx /var/log/nginx

安装依赖包
[root@localhost ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++
[root@localhost ~]# yum -y groups mark install 'Development Tools'

下载nginx安装包,并编译安装
[root@windfantasy ~]# wget http://nginx.org/download/nginx-1.14.2.tar.gz
[root@windfantasy ~]# tar xf nginx-1.14.2.tar.gz
[root@windfantasy nginx-1.14.2]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-debug \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_image_filter_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--http-log-path=/var/log/nginx/access.log \
--error-log-path=/var/log/nginx/error.log
root@windfantasy nginx-1.14.2]# make && make install

关闭防火墙和selinux
[root@windfantasy ~]# systemctl stop firewalld
[root@windfantasy ~]# setenforce 0 

设置nginx配置文件,当url匹配到.php结尾时,调用远程的php-fpm服务来解析,脚本文件的路径需设为和远程存放php文件的路径一致
[root@windfantasy ~]# vim /usr/local/nginx/conf/nginx.conf
location  / {
            root html;
            index index.php index.html index.htm;
        }
location ~ \.php$ {
            root           html;
            fastcgi_pass   192.168.43.129:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/apache/htdocs/wml.com$fastcgi_script_name;
            include        fastcgi_params;
        }
在/usr/local/nginx/html下创建空的index.php文件,让nginx服务可以找到即可
 [root@windfantasy html]# touch index.php
[root@windfantasy html]# chown nginx.nginx index.php 
[root@windfantasy ~]# nginx 

验证
在这里插入图片描述

通过代理将请求发给远程处理(以zabbix为例)

  • 远程端
启用apache,mysql,zabbix服务
[root@windfantasy wml.com]# apachectl start
[root@windfantasy wml.com]# service mysqld start
Starting MySQL SUCCESS! 
[root@windfantasy wml.com]# zabbix_server 
[root@windfantasy wml.com]# zabbix_agentd 

apahce配置文件如下
[root@windfantasy wml.com]# vim /etc/httpd24/httpd.conf 
<VirtualHost *:80>
    DocumentRoot "/usr/local/apache/htdocs/zabbix"
    ServerName www.zabbix.com
    ProxyRequests Off
    ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/usr/local/apache/htdocs/zabbix/$1
    <Directory "/usr/local/apache/htdocs/zabbix.com">
        Options none
        AllowOverride none
        Require all granted
    </Directory>
</VirtualHost>

  • 本机

设置nginx配置文件,指定请求给的远程主机域名
[root@windfantasy html]# vim /usr/local/nginx/conf/nginx.conf
location  / {
            root html;
            index index.php index.html index.htm;
            proxy_pass http://www.zabbix.com;
        }

重启服务
[root@windfantasy html]# nginx -s reload

在windows主机上添加hosts,将域名指定到nginx主机的ip地址,便于后面的的验证
在这里插入图片描述

  • 验证
  • 可以看到,zabbix服务是配置在远程主机上,输入域名后,由于hosts的设置,浏览器是访问nginx主机的;现在可以访问到zabbix界面,说明nginx的反向代理配置成功
    在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_44433314/article/details/88073717
今日推荐