2018-08-23

使用nginx转发知乎日报的api请求

最近在使用vux实现一个知乎日报,但是在请求知乎日报的api时,是跨域,这就需要使用nginx做反向代理,实现请求转发

知乎日报有开放的api接口,可以获取日报的主题列表,文章列表,文章详情等等信息。比如 http://news-at.zhihu.com/api/3/theme/2 可以获取到主题列表。

在自己的电脑上(win10)配置nginx环境,用winsw把nginx安装成服务

下载window下的nginx包,解压到C盘根目录,然后下载 winsw(我下载的是 winsw-2.1.2-bin.exe),然后再nginx目录下新建一个winsw目录,将winsw的exe文件放进去,添加一个同名的xml文件(我的为 winsw-2.1.2-bin.xml),将以下内容粘贴进去

<?xml version="1.0" encoding="UTF-8" ?>
<service>
 <id>nginx</id>
  <name>nginx</name>
  <description>nginx</description>
  <env name="path" value="C:/nginx"/>
  <executable>C:/nginx/nginx.exe</executable>
  <arguments>-p C:/nginx</arguments>
  <logpath>C:/nginx/logs/</logpath>      
  <logmode>roll</logmode>
</service>

用cmd(不要用gitbash或者powershell,会报错)进入到此目录,然后运行 winsw-2.1.2-bin.exe install

2018-06-25 23:21:07,766 INFO  - Installing the service with id 'nginx'

提示安装成功,接着执行 net start nginx 会出现nginx启动成功,如果报错,可能是端口问题,打开nginx目录下conf>nginx.conf文件,就 listen 80 改成其它端口,然后浏览器打开 localhost:端口号 ,会打开nginx的欢迎页面。

将指定请求进行转发

在conf目录下新建proxy_zhihu.conf文件,内容如下:

## Basic reverse proxy server ##  
## backend for 16.32  ##  
upstream uicps  {    
     server news-at.zhihu.com;  
}  
  
## Start 16.32 ##  
server {  
    listen 8081;  
    server_name  localhost;
  
#    access_log  logs/proxy34.access.log  main;  
#    error_log  logs/proxy34.error.log;   
  
    ## send request back to 16.32 ##  
    location /api/ {  
        resolver 223.5.5.5 223.6.6.6 1.2.4.8 114.114.114.114 valid=3600s;
        proxy_pass  http://uicps;  
  
        #Proxy Settings  
        proxy_redirect     off;  
        proxy_set_header   Host  news-at.zhihu.com;
        proxy_set_header   X-Real-IP        $remote_addr;  
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;  
        proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;  
        proxy_max_temp_file_size 0;  
        proxy_connect_timeout      90;  
        proxy_send_timeout         90;  
        proxy_read_timeout         90;  
        proxy_buffer_size          4k;  
        proxy_buffers              4 32k;  
        proxy_busy_buffers_size    64k;  
        proxy_temp_file_write_size 64k;  
   }  
}  

修改nginx.conf,将原来注释的内容去掉,在http下添加 include proxy_zhihu.conf;,修改后如下:

worker_processes  1;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    include       proxy_zhihu.conf;  
    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    server {
        listen       8080;
        server_name  localhost;

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

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

     }
}

接着用 net stop nginx 停止nginx服务,然后再用启动指令启动。浏览器中请求api,得到相同的结果。 请求对比图

服务器上配置

按照 windows server配置防火墙允许nginx访问外网,然后重启服务,然后就成功了。 修改conf目录下proxy_zhihu.conf文件,添加允许跨域,修改后如下

## Basic reverse proxy server ##  
## backend for 16.32  ##  
upstream uicps  {    
     server news-at.zhihu.com;  
}  
  
## Start 16.32 ##  
server {  
    listen 8081;  
    server_name  localhost;
  
#    access_log  logs/proxy34.access.log  main;  
#    error_log  logs/proxy34.error.log;   
  
    ## send request back to 16.32 ##  
    location /api/ {  
        resolver 223.5.5.5 223.6.6.6 1.2.4.8 114.114.114.114 valid=3600s;
        proxy_pass  http://uicps;  

        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Credentials' 'true';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';  

        #Proxy Settings  
        proxy_redirect     off;  
        proxy_set_header   Host  news-at.zhihu.com;
        proxy_set_header   X-Real-IP        $remote_addr;  
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;  
        proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;  
        proxy_max_temp_file_size 0;  
        proxy_connect_timeout      90;  
        proxy_send_timeout         90;  
        proxy_read_timeout         90;  
        proxy_buffer_size          4k;  
        proxy_buffers              4 32k;  
        proxy_busy_buffers_size    64k;  
        proxy_temp_file_write_size 64k;  
   }  
}