Skip to content
Go back

nginx基础

常用命令

start nginx.exe 
nginx.exe -s stop 
nginx.exe -s reload

正向代理

我想访问一个网站A,但是我不能直接访问它 代理服务器B可以直接访问A 于是我去访问B,让B帮我访问A,并且把A的内容返回来

反向代理

我去访问fuck.com/hentai这个页面,fuck.com其实没有hentai这个页面,但是他偷偷从另外一台服务器上取回来,然后作为自己的内容吐给用户,用户本身是毫不知情的

反向代理常用来解决跨域

跨域

除了同域名,其他情况都是跨域!

url说明跨域
http://www.b.com/b.js
http://www.a.com/a.js
不同域名
http://www.b.com/b.js
http://www.b.com/c.js
同一域名下不同文件夹
http://www.a.com/a.js
http://www.a.com:8000/a.js
同一域名,不同端口
http://www.a.com/a.js
https://www.a.com/a.js
同一域名,不同协议
http://www.a.com/a.js
http://70.32.92.74/b.js
域名和域名对应ip
http://www.a.com/a.js
http://script.a.com/b.js
主域相同,子域不同
http://www.a.com/a.js
http://a.com/b.js
同一域名,不同二级域名

比如我本地开了一个nginx服务器,访问:http://localhost:9090/bilibili.html

server {
        listen          9090;
        server_name     localhost;

        location / {
            root   html;
            index  index.html index.htm;
        }
    }

bilibili.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script src="//cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script>
</head>
<body>
    <h1>bilibli</h1>
    <script type="text/javascript">
        $.ajax({
            url: 'http://www.bilibili.com/index/catalogy/24-3day.json',
            type: 'GET',
            dataType: 'json',
        })
        .done(function(data) {
            console.log(data)
        })
        .fail(function(err) {
            console.log('err');
        })
    </script>
</body>
</html>

你会发现,数据无法获取,因为 http://www.bilibili.com 对于 localhost 来说是跨域的


我们可以添加一个代理

server {
        listen          9090;
        server_name     localhost;

        location / {
            root   html;
            index  index.html index.htm;
        }

        location /apis {
            include  uwsgi_params;
            proxy_pass   http://www.bilibili.com/index/catalogy/24-3day.json;
        }
    }
$.ajax({
            url: '/apis',
            type: 'GET',
            dataType: 'json',
        })

这时,访问localhost:9090/apis就等于访问http://www.bilibili.com/index/catalogy/24-3day.json 并且不存在跨域了

注意,如果你想这样代理

$.ajax({
            url: '/apis/24-3day.json',
            type: 'GET',
            dataType: 'json',
        })

那么要这样配置

location /apis/ {
            include  uwsgi_params;
            proxy_pass   http://www.bilibili.com/index/catalogy/;
        }

虚拟主机

server {
        listen       8899;
        server_name  hentai.com;

        location / {
            root   html;
            index  index.html index.htm;
       } 
    }

然后更改hosts文件,建议使用SwitchHosts

127.0.0.1 hentai.com

于是访问hentai.com:8899就和访问localhost:8899一样了

常见的开发模式

比如,我用xampp开启了8080端口,然后用nginx替我转发接口跨域

server {
        listen          9090;
        server_name     localhost;

        location / {
            proxy_pass  http://127.0.0.1:8080;
        }

        location /apis {
            include  uwsgi_params;
            proxy_pass   http://www.bilibili.com/index/catalogy;
        }
    }

源代码是在xampp的目录里,但是访问是通过localhost:9090(nginx)访问


Share this post on:

Previous Post
node服务器基础
Next Post
Cmder