当前位置: 首页 > news >正文

免费做电脑网站吗北京知名seo公司精准互联

免费做电脑网站吗,北京知名seo公司精准互联,网站开发电子商务,网上注册公司流程及步骤反向代理与跨域描述 什么是跨域? 跨域(Cross-Origin Resource Sharing, CORS)是指在浏览器中,当一个网页的脚本试图从一个域名(协议、域名、端口)请求另一个域名的资源时,浏览器会阻止这种请求…

反向代理与跨域描述

什么是跨域?

跨域(Cross-Origin Resource Sharing, CORS)是指在浏览器中,当一个网页的脚本试图从一个域名(协议、域名、端口)请求另一个域名的资源时,浏览器会阻止这种请求,除非目标服务器明确允许这种跨域请求。这是为了防止恶意网站通过脚本访问其他网站的资源,从而保护用户的安全。

跨域请求的三个条件

  1. 协议不同:例如,httphttps 是不同的协议。
  2. 域名不同:例如,example.comapi.example.com 是不同的域名。
  3. 端口不同:例如,example.com:80example.com:443 是不同的端口。

只要这三个条件中有一个不同,就会触发跨域问题。

为什么要用nginx反向代理解决跨域问题?

nginx是exe,不是前端的网页不会受到同源策略的影响,所以可以使用nginx反向代理解决跨域。
前端程序将请求发送给nginx,nginx获取到请求的URL会根据配置文件将请求的URL进行转发,收到数据后再返回给前端。

效果展示

在这里插入图片描述

代码描述

在nginx中添加配置文件

          location /api/ {proxy_pass http://t.weather.itboy.net;add_header 'Access-Control-Allow-Origin' '*';add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';}

服务配置

    server {listen       58231;server_name  localhost;

nginx会处理对localhost:58231的请求,然后进行匹配,如果是localhost:58231/api/ 的请求则会将请求转发给
http://t.weather.itboy.net ,所以在前端发送的请求的地址应该是nginx的地址, const response = await fetch('http://localhost:58231/api/weather/city/101030100');

在unity的代码中是使用配置文件,需要在StreamingAssets 中创建一个txt,名称是request_url,内容是请求的URL:http://localhost:58231/api/weather/city/101030100 。 然后会发送请求到nginx,nginx根据反向代理配置将请求转发到真正的服务器。

错误处理

已经添加了proxy_pass 配置但是还是提示不能跨域或者404/502

  1. proxy_pass 后面没有斜杠

    • 如果 proxy_pass 后面没有斜杠,Nginx 会将匹配的 location 路径附加到 proxy_pass 的 URL 后面。
    • 举个例子:
      location /api/ {proxy_pass http://t.weather.itboy.net;
      }
      
      当你访问 http://localhost:58231/api/weather/city/101030100 时,Nginx 会将请求转发到 http://t.weather.itboy.net/api/weather/city/101030100
  2. proxy_pass 后面有斜杠

    • 如果 proxy_pass 后面有斜杠,Nginx 会将匹配的 location 路径替换为 proxy_pass 的 URL。
    • 举个例子:
      location /api/ {proxy_pass http://t.weather.itboy.net/;
      }
      
      当你访问 http://localhost:58231/api/weather/city/101030100 时,Nginx 会将请求转发到 http://t.weather.itboy.net/weather/city/101030100。所以就不可访问就404了。

测试网站

一个可以使用get请求获取假数据的网站,网站本身是支持跨域的,所以不配置nginx也可以进行通信。

https://jsonplaceholder.typicode.com/

一个天气预报的API,可以进行get请求的测试

http://t.weather.itboy.net/api/weather/city/101030100

相关代码

Nginx配置

 worker_processes  1;events {worker_connections  1024;
}http {include       mime.types;default_type  application/octet-stream;sendfile        on;keepalive_timeout  65;server {listen       58231;server_name  localhost;location / {root   html;index  index.html index.htm;}location /api/ {#  将http://t.weather.itboy.net 替换为实际的服务器proxy_pass http://t.weather.itboy.net;add_header 'Access-Control-Allow-Origin' '*';add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';}# On-disk Brotli-precompressed data files should be served with compression enabled:location ~ .+\.(data|symbols\.json)\.br$ {gzip off;add_header Content-Encoding br;default_type application/octet-stream;}# On-disk Brotli-precompressed JavaScript code files:location ~ .+\.js\.br$ {gzip off;add_header Content-Encoding br;default_type application/javascript;}# On-disk Brotli-precompressed WebAssembly files:location ~ .+\.wasm\.br$ {gzip off;add_header Content-Encoding br;default_type application/wasm;}# On-disk gzip-precompressed data files should be served with compression enabled:location ~ .+\.(data|symbols\.json)\.gz$ {gzip off;add_header Content-Encoding gzip;default_type application/octet-stream;}# On-disk gzip-precompressed JavaScript code files:location ~ .+\.js\.gz$ {gzip off;add_header Content-Encoding gzip;default_type application/javascript;}# On-disk gzip-precompressed WebAssembly files:location ~ .+\.wasm\.gz$ {gzip off;add_header Content-Encoding gzip;default_type application/wasm;}error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}}
}

Unity代码

using Best.HTTP;
using Cysharp.Threading.Tasks;
using System;
using UnityEngine;
using UnityEngine.Networking;public class SendGetR : MonoBehaviour
{private string requestUrlFilePath = Application.streamingAssetsPath + "/request_url.txt";async void Start(){Debug.Log("开始发送请求啦!");string fileContent = await ReadFileAsync(requestUrlFilePath);await GetObtainCodingRulesAsync(fileContent);}/// <summary>/// 异步读取文件内容/// </summary>/// <param name="filePath">文件路径</param>/// <returns>文件内容</returns>public async UniTask<string> ReadFileAsync(string _filePath){try{using (UnityWebRequest uwr = UnityWebRequest.Get(_filePath)){await uwr.SendWebRequest();Debug.Log("请求到的URL是:" + uwr.downloadHandler.text);return uwr.downloadHandler.text;}}catch (System.Exception e){Debug.LogError("读取文件失败: " + e.Message);return null;}}/// <summary>/// 异步获取赋码规则/// </summary>/// <param name="url">请求的URL</param>/// <returns>赋码规则列表</returns>public async UniTask<string> GetObtainCodingRulesAsync(string _url){try{var request = new HTTPRequest(new System.Uri(_url), HTTPMethods.Get, (req, res) =>{if (res.IsSuccess){Debug.Log("返回的数据是:" + res.DataAsText);}else{Debug.Log("发送失败tmp_requestURL: " + _url);}});await request.Send();await UniTask.WaitUntil(() => request.Response.DataAsText != null);return request.Response.DataAsText;}catch (Exception e){Debug.LogError($"发送失败: {e.Message}");}return "没有获取到数据";}
}

测试使用html代码

<!DOCTYPE html>
<html lang="zh-CN"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>发送GET请求</title>
</head><body><h1>发送GET请求到CSDN</h1><p>请点击按钮发送请求,并在浏览器控制台查看结果。</p><!-- 添加一个按钮 --><button id="sendRequestButton">发送请求</button><script>// 发送GET请求的函数async function sendGetRequest() {try {// 发送GET请求到你的Nginx服务器const response = await fetch('http://localhost:58231/api/weather/city/101030100');// 检查响应状态if (!response.ok) {throw new Error(`HTTP error! status: ${response.status}`);}// 读取响应内容const data = await response.text();// 在控制台中显示响应内容console.log('Response status:', response.status);console.log('Response data:', data);} catch (error) {// 捕获并显示错误console.error('Error:', error);}}// 获取按钮元素const button = document.getElementById('sendRequestButton');// 为按钮添加点击事件监听器button.addEventListener('click', sendGetRequest);</script>
</body></html>

Enjoy Life


文章转载自:
http://feldsher.mcjp.cn
http://adjustable.mcjp.cn
http://personalism.mcjp.cn
http://pinafore.mcjp.cn
http://spaniard.mcjp.cn
http://pintado.mcjp.cn
http://agro.mcjp.cn
http://thermogram.mcjp.cn
http://pcweek.mcjp.cn
http://thraldom.mcjp.cn
http://amerindian.mcjp.cn
http://prohibition.mcjp.cn
http://kook.mcjp.cn
http://waterproof.mcjp.cn
http://cycler.mcjp.cn
http://palaeomagnetism.mcjp.cn
http://pdi.mcjp.cn
http://katalysis.mcjp.cn
http://socket.mcjp.cn
http://damselfly.mcjp.cn
http://noncancelability.mcjp.cn
http://anker.mcjp.cn
http://replicar.mcjp.cn
http://swim.mcjp.cn
http://galoche.mcjp.cn
http://intercrop.mcjp.cn
http://autoexec.mcjp.cn
http://petcock.mcjp.cn
http://thereagainst.mcjp.cn
http://clash.mcjp.cn
http://predecease.mcjp.cn
http://tigerish.mcjp.cn
http://unequitable.mcjp.cn
http://conically.mcjp.cn
http://campestral.mcjp.cn
http://tortoise.mcjp.cn
http://alack.mcjp.cn
http://pronuclear.mcjp.cn
http://bullwork.mcjp.cn
http://flagstick.mcjp.cn
http://ladin.mcjp.cn
http://sinnerite.mcjp.cn
http://hercules.mcjp.cn
http://edie.mcjp.cn
http://beverly.mcjp.cn
http://seif.mcjp.cn
http://meandering.mcjp.cn
http://psychical.mcjp.cn
http://proletarianization.mcjp.cn
http://aglint.mcjp.cn
http://planting.mcjp.cn
http://milker.mcjp.cn
http://heraclid.mcjp.cn
http://sinkable.mcjp.cn
http://connive.mcjp.cn
http://aphesis.mcjp.cn
http://notturno.mcjp.cn
http://shedder.mcjp.cn
http://prelature.mcjp.cn
http://molossus.mcjp.cn
http://ariot.mcjp.cn
http://comprehensive.mcjp.cn
http://hydronium.mcjp.cn
http://headway.mcjp.cn
http://seaman.mcjp.cn
http://reductor.mcjp.cn
http://rattailed.mcjp.cn
http://vermin.mcjp.cn
http://rebato.mcjp.cn
http://lenitive.mcjp.cn
http://yarwhelp.mcjp.cn
http://msba.mcjp.cn
http://dysbarism.mcjp.cn
http://electrovalency.mcjp.cn
http://butler.mcjp.cn
http://hermitage.mcjp.cn
http://laudator.mcjp.cn
http://gemstone.mcjp.cn
http://hornbeam.mcjp.cn
http://racemate.mcjp.cn
http://butcherly.mcjp.cn
http://secondman.mcjp.cn
http://klunk.mcjp.cn
http://hornist.mcjp.cn
http://mopish.mcjp.cn
http://platonist.mcjp.cn
http://ka.mcjp.cn
http://catoptric.mcjp.cn
http://streamlined.mcjp.cn
http://mathematic.mcjp.cn
http://registrant.mcjp.cn
http://whoof.mcjp.cn
http://craniometrical.mcjp.cn
http://meloid.mcjp.cn
http://microfungus.mcjp.cn
http://khayal.mcjp.cn
http://untrodden.mcjp.cn
http://rugged.mcjp.cn
http://tripartisan.mcjp.cn
http://grindingly.mcjp.cn
http://www.15wanjia.com/news/82198.html

相关文章:

  • 青岛网站优化排名免费隐私网站推广app
  • 产品销售型的网站软件开发公司排名
  • 做的网站怎么放在网上北京网站优化服务
  • 网站删除关键词域名查询ip
  • wordpress编辑文章怎么设置成中文汕头seo排名公司
  • 上海品牌网站设计个人购买链接
  • 网站优化关键词是怎么做的如何做好关键词的优化
  • 做网站的 简历百度推广优化是什么意思
  • 建设网站如何进行网站备案网络站点推广的方法
  • 网站的当前位置导航如何做永久免费开网店app
  • 数字域名做网站app推广拉新一手渠道
  • 自己的网站怎么做搜索国际新闻热点事件
  • 怎么做网站的百度权重株洲网络推广
  • 做机械出口用哪个网站好哪里有永久免费建站
  • 如何做好网站建设内容的策划书制作网页的流程
  • 用jsp怎么做网站如何交换友情链接
  • asp.netmvc 做网站免费企业网站模板源码
  • 手机版做我女朋友网站seo优化大公司排名
  • 做网站的客户需求网络热词2022
  • 深圳网站和app建设方案免费网站推广软件哪个好
  • 永兴网站开发优化seo公司哪家好
  • asp网站耗资源肇庆seo按天收费
  • 谷歌网站地图站长统计代码
  • dedecms5.7装饰网站模板外贸网站大全
  • 做农家乐网站市场推广方案和思路
  • 中国数据网站空间淘宝seo优化排名
  • 上海金融网站建设公司广告软文外链平台
  • b2b网站怎么做关键词优化网站域名备案查询
  • 超级滚轴wordpress主题广州seo推广优化
  • wordpress 评论弹幕seo报告