nginx反向代理

1.本文静态文件存在本地目录,其余数据由远程服务提供

2.Nginx配置如下

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;

    server {
        listen       80;
        server_name  192.168.98.128:8080;
        #静态文件存在桌面
        location ~ \.(html|js|css|png|gif)$ {
            root   C:/Users/jh/Desktop;
        }
        #匹配demo开头的url,服务由远程的spring-boot提供
        location /demo/ {
            proxy_pass   http://192.168.98.128:8080/demo/;
        }
    }
}

3.在nginx目录下启动nginx,start nginx(关闭服务 命令,nginx -s stop)

4.访问如下

浏览器访问 http://localhost/test.html

这里写图片描述
访问http://localhost/demo/emp/name

这里写图片描述

5.test.html文件如下

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://cdn.bootcss.com/jquery/1.10.2/jquery.min.js">
</script>
<script>

$(document).ready(function(){
    $("button").click(function(){
    alert("hello world");
        $.get("./demo/emp/name",function(data,status){
            alert("数据: " + data + "\n状态: " + status);
    $("#test3").val(data);
        });
    });
});
</script>
</head>
<body>

<button>HTTP GET</button>
<input type="text" id="test3" value="hello world"></p>

</body>
</html>

点击按钮,能够获取后台数据,实现跨域

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值