Apache Nginx反向代理解决跨域问题

1.Apache

1.启用proxy模块
Windows:修改httpd.conf配置文件,去掉以下两行前面 # 号,从而启用Apache proxy module。

LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so

Ubuntu:

sudo apt-get install libapache2-mod-proxy-html
sudo apt-get install libxml2-dev
sudo a2enmod proxy proxy_http

2.配置virtualHost


<VirtualHost *:80>
   ProxyRequests Off
    <Proxy *>
      Order deny,allow
      Allow from all
    </Proxy>
    ProxyPass /apis http://ip_address/apis
    ProxyPassReverse /apis http://ip_address/apis
</VirtualHost>

1.ProxyRequests Off 指令是指采用反向(reverse)代理
2.ProxyPass 指令允许将一个远端服务器映射到本地服务器的URL空间中
3.ProxyPassReverse 它一般和ProxyPass指令配合使用,此指令使Apache调整HTTP重定向应答中Location, Content-Location, URI头里的URL,这样可以避免在Apache作为反向代理使用时,后端服务器的HTTP重定向造成的绕过反向代理的问题
4.配置完成之后,访问 http://localhost/apis 实际就是访问 http://ip_address/apis上的资源

2.Nginx

配置server

server {
        listen       80;
        server_name  localhost;
        charset utf8;
        access_log  /var/log/nginx_api/access.log;
        error_log /var/log/nginx_api/error.log;
        location / {
            root   html;
            index  index.html index.htm;
        }
        location /apis {
            rewrite  ^.+apis/?(.*)$ /$1 break;
            include  uwsgi_params;
            proxy_pass   http://ip_address/apis;
        }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值