Nginx解决跨域(详细)

Nginx解决跨域

自己总结一下,加深记忆,相信下图的情况很多人都遇到过,具体产生原因我就不详讲了.
在这里插入图片描述
一.为了不在nginx.conf做太多改动,自己创建一个配置文件,并在nginx.conf配置导入;
1.创建自定义配置文件.
在这里插入图片描述
自定义配置文件内容:

server
{
   listen 3003;
   server_name localhost;
   ## alias属性值为前端项目根路径
   ## 定义alias属性作用是:当访问localhost:3003/index.html相当于访问 D:/r/work/hbulider/demo/demo1/test/路径下的index.html;
   location / {
      alias  D:/r/work/hbulider/demo/demo1/test/;
   }
   ##  /achdev 表示以/achdev 开头的url,包括/achdev 1,/achdev/son,或者/achdev/son/grandson
   ##  实际访问为http://192.168.27.132:8084/achdev开头的url
   location /achdev {
	   proxy_pass http://192.168.27.132:8084; #请求代理地址
   }
   ##  /ok/表示精确匹配以ok开头的url,/achdev是匹配不到的,/ok/son则可以
   location /ok/ {
       proxy_pass http://localhost:3000;
   }
}

导入方式:在nginx.conf里的http里通过include指定导入配置文件
在这里插入图片描述
三.通过localhost:3003/index.html访问
在这里插入图片描述
访问成功!!!

总结:
一.Nginx可以监听多个端口
配置方式:
1.在server段写上2个Listen就可以了.
2.在 nginx.conf 中配置多个个server即可

二.location属性:
Nginx设置alias实现虚拟目录 alias与root的用法区别

  1. location ~ ^/awstats/ {
    root /home/awstats/;
    }
    访问:http://test.com/awstats/ 实际访问的是/home/awstats/awstats/
  2. location ~ ^/awstats/ {
    alias /home/
    }
    访问:http://test.com/awstats/ 实际访问的是/home/
  3. location ~ ^/awstats/ {
    #使用alias时目录名后面一定要加“/”
    alias /home/awstats/;
    }

访问:http://test.com/awstats/ 实际访问的是/home/awstats/
4.location ~ ^/awstats/ {
root /home/;

}
访问:http://test.com/awstats/ 实际访问的是/home/awstats/
总结:alias是替换,root是加指定前缀

代理路径:
proxy_pass http://192.168.27.132:8084;

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值