美文网首页
Nginx实现跨域

Nginx实现跨域

作者: 我的钱包瘪瘪的 | 来源:发表于2020-03-16 11:07 被阅读0次

Nginx下载

  1. 概念
    1. 高性能web服务器
    2. 一般用作静态服务器, 负载均衡
    3. 反向代理(跨域)


    nginx.png
  2. 下载

    1. win下载: 官网下载http://nginx.org/en/download.html
    2. mac下载: brew install nginx
  3. 配置文件

    1. win: C:\nginx\conf\nginx.conf
    2. mac: /usr/local/etc/nginx/nginx.conf
  4. Nginx命令

    1. 检测配置文件格式是否正确: nginx -t
    2. 启动nginx, 重启nginx: nginx -s reload
    3. 停止: nginx -s stop
    4. Mac 打开配置文件: sudo vi /usr/local/etc/nginx/nginx.conf(也可直接编辑)

Nginx实现跨域

第一步 node服务端

    const http = require('http')
    const url = require('url')
    const server = http.createServer((req, res) => {
      const { method } = req
      const { pathname } = url.parse(req.url)
      res.writeHead(200, { 'Content-Type': 'text/plain; charset=utf-8' })
      if (pathname === '/list' && method === 'GET') {
        const data = JSON.stringify({ username: 'test', pwd: 123 })
        console.log(pathname);
        res.end(data)
      } else {
        res.writeHead(404, { 'Content-Type': 'text/plain; charset=utf-8' })
        res.end('404 not found')
      }
    })
    server.listen(3000, () => {
      console.log('listen port at 3000')
    })

第二步 web客户端

    <!--通过http-server -p 3001 将客户端启动-->
    <body>
      测试页面
      <script src="https://cdn.bootcss.com/axios/0.19.2/axios.js"></script>
      <script>
        axios.get('http://localhost:3002/list').then(data => {
          console.log(data)
        })
      </script>
    </body>

第三步 配置nginx

    # 设定http服务器, 之后请求都是http不是https
    http { 
        server {
            listen 3002;  # 侦听3002端口
            server_name  localhost;    # 客户端域名
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Credentials' 'true'; 
            add_header 'Access-Control-Allow-Methods' 'GET,POST,OPTIONS';
            # 静态资源的地址, 可不配置也能实现跨域
            # location / {
            #     root   C:/Users/80021648/Desktop/js;  #定义服务器的默认网站根目录位置
            #     index  index.html index.htm app.html;  #定义首页索引文件的名称
            # }
            location /list{
                proxy_pass http://localhost:3000/list; # 代理转发的服务端地址
            }
        }
    }

总结

    服务端: http://localhost:3000
    客户端: http://localhost:3001
    nginx: http://localhost:3002
    
    客户端请求nginx监听的端口,  nginx监听到端口请求, 然后代理转发的服务端地址
    
    客户端 -> 服务端
    axios.get('http://localhost:3000/list')
    变成 客户端 -> nginx代理转发 -> 服务端
    axios.get('http://localhost:3002/list')

相关文章

  • ajax跨域--nginx反向代理

    用nginx反向代理实现跨域,是最简单的跨域方式。只需要修改nginx的配置即可解决跨域问题,支持所有浏览器,支持...

  • 4种方法解决js跨域的实现方式

    一、JSONP 使用ajax实现跨域: 二、CORS跨域资源共享 三、Nginx反向代理 四、webpack (在...

  • Nginx实现跨域

    Nginx下载 概念1. 高性能web服务器2. 一般用作静态服务器, 负载均衡3. 反向代理(跨域)nginx....

  • nginx实现跨域

    问题描述 在本地用create-react-app起一个服务(http://localhost:3000[http...

  • nginx解决跨域

    使用nginx实现跨域 【转载】 原文: https://www.cnblogs.com/lovesong/p/1...

  • 浏览器跨域的那些事

    整理中 目标: 了解跨域 解决跨域 服务器配置跨域(java, nginx) 前端调试时配置解决跨域 一、什么是跨...

  • nginx反向代理

    前端调用接口的时候跨域了怎么办呢,如下代码跨域: 通过nginx反向代理下载nginx:http://nginx....

  • 被调用方解决

    被调用方解决 1、被调用方解决 – 支持跨域 (1)被调用方支持跨域解决方案 服务端实现 Nginx配置 Apac...

  • 关于设置env等环境变量的思考

    1、如何处理跨域后台处理跨域前端处理跨域浏览器处理跨域 前端本地处理跨域:代理线上跨域的处理方式:Nginx反向代...

  • Nginx跨域

    Nginx解决跨域问题(CORS) CORS(Cross-Origin Resource Sharing) 跨域资...

网友评论

      本文标题:Nginx实现跨域

      本文链接:https://www.haomeiwen.com/subject/sdawehtx.html