跨域访问方法-Nginx反向代理

1. nginx安装

(1) 安装pcre

下载链接:https://sourceforge.net/projects/pcre/files/latest/download?source=files

下载后解压压缩包,进入解压后文件夹的目录,打开终端。

运行命令:$ sudo ./configure --prefix=/usr/local
        $ sudo make $ sudo install

(2) 安装nginx

下载链接:

Linux:http://nginx.org/download/nginx-1.10.1.tar.gz

windows:http://nginx.org/download/nginx-1.10.1.zip

下载后解压压缩包,进入解压后文件夹的目录,打开终端。

运行命令:$ sudo ./configure --prefix=/usr/local/nginx
 --with-cc-opt="-Wno-deprecated-declarations"
        $ sudo make $ sudo install

配置环境变量:
运行命令: $ touch ~/.bash_profile
        $ open ~/.bash_profile
编辑bash_profile文件,向PATH新增nginx变量:
    export PATH=$PATH:/usr/local/nginx/sbin

2. nginx操作

(1) 启动nginx:

运行命令:$ nginx

打开浏览器http://localhost,看到以下页面则表明nginx运行正常

                    Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

(2) 关闭nginx:

运行命令:$ nginx -s stop

(3) 重启nginx:

运行命令:$ nginx -s reload

3. nginx 反向代理配置

(1) 进入nginx的conf目录:

运行命令:$ cd /usr/local/nginx/conf

(2) 修改nginx.conf文件:

运行命令:$ vi nginx.conf
修改如下内容:
   server {
    listen       9000;
    server_name  localhost;
    autoindex off;
    index index.html index.htm index.php;
    #charset koi8-r;

    #access_log  logs/host.access.log  main;

    location / {
        #root   html;
        proxy_pass http://localhost:8080/;
        index  index.html index.htm;
    }

    location /BCYS-Market-biz/ {
        #rewrite ^.+api/?(.*)$ /BCYS-Market-biz/$1 break;
        proxy_pass http://120.76.97.189:21080;

    }
  }

 保存后退出

(3) 重启nginx

参考资料:

  1. 用nginx的反向代理机制解决前端跨域问题:http://www.w2bc.com/Article/86876
  2. nginx配置location总结及rewrite规则写法:http://seanlook.com/2015/05/17/nginx-location-rewrite/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值