您的位置:首页 > 运维架构 > 反向代理

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

2016-06-28 17:37 555 查看

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

参考资料:

用nginx的反向代理机制解决前端跨域问题:http://www.w2bc.com/Article/86876

nginx配置location总结及rewrite规则写法:http://seanlook.com/2015/05/17/nginx-location-rewrite/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: