美文网首页
通过nginx将本地请求打到开发机上

通过nginx将本地请求打到开发机上

作者: 淡淡的橙子 | 来源:发表于2018-04-12 21:37 被阅读0次

现在的工作的由于环境的隔离,所以开发需要在开发机上进行开发。
本地调试时,如果希望将请求打到开发机上起的本地服务,在windows下可以通过fiddler来进行,但是在mac下由于不存在像fiddler般简单易用的软件,所以可以使用比如nginx来进行转发。

配置起来需要如下几部:

  • 安装homebrew:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
  • 安装nginx:
    sudo brew install nginx
    需要注意的是,有可能需要将homebrew的路径设置对应用户的权限,否则在安装的时候会产生问题。

  • 配置nginx文件:

worker_processes  1;

error_log  /usr/local/var/log/nginx/logs/error.log;
error_log  /usr/local/var/log/nginx/logs/error.log  notice;
error_log  /usr/local/var/log/nginx/logs/error.log  info;

pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    
    // 配置日志格式
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    
    // 需要注意这个main要和日志格式的main相对应
    access_log  /usr/local/var/log/nginx/logs/access.log  main;

    sendfile  on;

    keepalive_timeout  65;

    server {
        // 配置resovler,即dns服务器
        resolver 172.22.1.253;
        // 监听80端口
        listen       80;
        server_name  crm-off.work-int.com;
        // 配置日志
        access_log  /usr/local/var/log/nginx/logs/host.access.log main;

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        
        // 配置转发规则
        location ~ /lacrm-cust/hs {
            proxy_pass http://10.99.200.178:8080;
        }
        
        // 不拦截的路径
        location / {
            proxy_pass http://$http_host;
        }

    }
}

对于nginx来讲,配置文件中关键的内容有以下几点:

日志中的內建变量
这篇文章介绍的比较详细
Nginx的日志配置参数详解

转发的规则
具体的转发规则location我们就不赘述了,有很多文章介绍。

dns的问题
在我们的场景中,我们需要一个比较特殊的情况是:我们需要拦截某些请求到本地,而希望其他的请求继续走原始的路径。但是由于我们通过了配置hosts将域名打到了本地127.0.0.1,所以如果在proxy_pass中继续配置http://crm-off.work-int.com的话,由于仍会走本机的host,所以仍然会打到本机的nginx上。所以我们需要绕过本机的host的配置。为了解决这个问题,我们需要了解下nginx对于域名的解析规则。nginx在加载配置文件的时候,会将host加载到上下文中,然后对于配置文件中的域名会进行解析。而如果我们不希望进行该解析,则需要通过内置变量来进行。比如$http_host。这就是我们在上面的配置文件中看到了:

location / {
 proxy_pass http://$http_host;
}

的原因。
以上的讨论,在文章 Nginx 教程二:利用nginx搭建静态文件服务、正向代理服务器、反向代理服务器也有讨论,称之为正向代理,与反向代理所区别。可以理解为一个为出,一个为入。
但是,单纯的如此编辑后,仍然会存在问题,比如域名解析不出来等。此时我们需要配上域名解析器,也就是server模块:

resolver 172.22.1.253;

那么有个问题就产生了,这个ip是如何产生的呢。

查找域名解析器
我们需要去查找我们的$http_host的dns服务器域名。这时候nslookup就出场了。
使用nslookup查找域名:

nslookup www.baidu.com
// dns服务器
Server:     172.22.1.253
// dns服务器ip+port,其中53是专门用于请求的port
Address:    172.22.1.253#53

www.baidu.com   canonical name = www.a.shifen.com.
Name:   www.a.shifen.com
Address: 180.149.132.151
Name:   www.a.shifen.com
Address: 180.149.131.98

于是我们便获得了dns的服务器。实际如果访问的是公网的话,则直接填8.8.8.8,是Google提供的免费DNS服务器的IP地址。

  • 启动nginx:
// 单纯的启动nginx,加载默认的配置文件
sudo nginx

// 检查配置文件是否正确
nginx -t -c /usr/nginx/conf/nginx.conf

// 按照配置文件启动
nginx -c /usr/nginx/conf/nginx.conf

// 重载配置文件
nginx -s reload

// 关闭nginx
nginx -s stop
nginx -s quit

其中stop和quit的区别在于quit是一种较平滑的退出。

相关文章

网友评论

      本文标题:通过nginx将本地请求打到开发机上

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