反代理
内容纲要

由于WEB开发的安全性问题,往往访问服务器的时候需要验证身份,而有些资源是不需要验证身份的,比如公共资源,这时最常用的做法就是开放服务器权限,以允许所有访问者都能访问到资源。
对于比较敏感的资源一般都不允许访问,或者需要验证属于同源才能访问。反代理就是去访问一些禁止跨域的资源。
需要软件:
NGINX
1. 介绍
关于介绍:
百度百科:nginx
2. 下载
官方下载地址:下载地址
3. 安装
略.
安装完成后先不要打开服务
4. 修改自己的服务器地址
- CD到软件安装根目录/conf/下,打开nginx.conf文件。
-
将listen端口修改为自己的端口
-
将server_name修改为自己的服务器地址
-
将server下的location下的默认入口修改为自己的服务器入口:
proxy_pass http://localhost:8090;
proxy_redirect default;
5. 修改需要访问的服务器地址
在server下增加一个指向服务器地址的路径:
location /apis {
#添加访问目录为/apis的代理配置
rewrite ^/apis/(.*)$ /$1 break;
proxy_pass http://192.168.1.107:8011;
}
修改完成后的完整文件内容:
server {
listen 8090;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
#root html;
#index index.html index.htm;
proxy_pass http://localhost:8090;
proxy_redirect default;
}
location /apis {
#添加访问目录为/apis的代理配置
rewrite ^/apis/(.*)$ /$1 break;
proxy_pass http://192.168.1.107:8011;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
6. 启动服务
- CD回到根目录
-
./start nginx启动服务(windows直接双击.exe文件)
7. 安装anywhere
使用npm全局安装anywhere:
npm i -g anywhere
使用命令:
anywhere -p8090
在任何目录下启动都会自动打开浏览器,并指定默认入口为当前命令目录。
note:
需要将项目中的根路径改为自己服务器的地址和端口。例如:const baseUrl = ‘http://192.168.1.100:8090/apis/服务器相对路径’;
code enjoy! ٩(๑❛ᴗ❛๑)۶
作者:indeex
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。