Nginx配置代理解决本地html进行ajax请求接口跨域问题

场景

Nginx在Windows下载安装启动与配置前后端请求代理:

Nginx在Windows下载安装启动与配置前后端请求代理_霸道流氓气质的博客-CSDN博客

上面基于Vue的web项目进行代理请求后台接口。

如果是进行异地接口联调,访问后台接口都需要通过vpn访问,前端需求是使用

单html页面(带其他js、三方sdk资源)进行接口联调调用,后续需将该静态html

资源封装到APP中。若线上环境网络均是内网,不存在跨域问题,在联调阶段

如何进行模拟请求对接。

注:

博客:
霸道流氓气质的博客_CSDN博客-C#,架构之路,SpringBoot领域博主

实现

1、通过vpn访问的第三方接口地址

 

假设这里的地址是

http://1.2.3.4:18079/cardata/api/GetCarRealData

2、在html中直接通过ajax请求

            $.ajax({
              type: 'get',
              url: 'http://1.2.3.4:18079/cardata/api/GetCarRealData',
              dataType: 'json',
              success: function (r) {
                if (r && r.code === 200) {     

                 }
              }
            })

在浏览器中转直接打开该本地html,请求时则会提示跨域问题。

3、所以需要使用nginx做一个请求的代理

修改nginx的配置文件

​
    server {
        listen       900;
        server_name  127.0.0.1;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   D:/test/lrtest/;
            try_files $uri $uri/ /index.html;
            index  wsindex.html index.htm;
        }
  
        location /cardata/api/ {
            proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header REMOTE-HOST $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://1.2.3.4:18079/cardata/api/;
        }
  
        # 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;
        #}
    }

​

这里将单html页面,这里是wsindex.html以及需要请求的js文件等放在d盘test/lrtest目录下

 

这里的

        location / {
            root   D:/test/lrtest/;
            try_files $uri $uri/ /index.html;
            index  wsindex.html index.htm;
        }


就是配置静态html的路径

然后配置请求代理

​
        location /cardata/api/ {
            proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header REMOTE-HOST $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://1.2.3.4:18079/cardata/api/;
        }

​

将/cardata/api/开头的请求代理到 http://1.2.3.4:18079/cardata/api/

然后保存配置文件,启动Nginx,访问

http://127.0.0.1:900/

可以发现ajax请求不再有跨域问题

 

  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Nginx作为反向代理服务器可以用来解决跨域问题。通过将本地的URL前缀映射到需要跨域访问的Web服务器上,Nginx可以将HTTP请求转发到后端的真实服务器上,并通过rewrite命令去掉前缀。这样,真实的服务器可以正确处理请求,而不知道请求来自代理服务器。\[2\] 在Nginx配置文件中,可以使用location指令来匹配需要跨域访问的路径,并在proxy_pass中填写需要访问的外域API地址。例如,当访问"http://localhost:8088/api/.*"路径时,可以将proxy_pass设置为"http://test.abc.com/",这样实际上访问的是"http://test.abc.com/.*",但在浏览器中显示的仍然是原来的路径"http://localhost:8088/api/.*"。需要注意的是,在匹配路径和代理路径的末尾都不能少斜杠,这是Nginx的规则。修改完nginx.conf文件后,需要重启Nginx才能生效。\[3\] 通过这种方式,Nginx可以实现跨域访问,解决跨域问题。 #### 引用[.reference_title] - *1* [Nginx 反向代理解决跨域问题](https://blog.csdn.net/weixin_43066287/article/details/121104183)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [ajax nginx 转发 sessionid_【Nginx】使用Nginx如何解决跨域问题?看完这篇原来很简单!!...](https://blog.csdn.net/weixin_39954698/article/details/109917716)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [nginx反向代理解决跨域请求问题](https://blog.csdn.net/lm____/article/details/120462636)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

霸道流氓气质

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值