Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nginx反向代理删除响应头 #1

Open
lushuhao opened this issue Dec 10, 2017 · 2 comments
Open

Nginx反向代理删除响应头 #1

lushuhao opened this issue Dec 10, 2017 · 2 comments

Comments

@lushuhao
Copy link
Owner

@lushuhao lushuhao commented Dec 10, 2017

最近仿照QQ音乐写web app,中间需要抓取接口,刚好买了一台云服务器可以用来做反向代理。解析了一个二级域名专门作为代理服务器。

首先在conf文件里,设置一个响应头给本地服务器跨域

add_header 'Access-Control-Allow-Origin' '*'; 

再来匹配一个location,来代理QQ音乐的服务器

location /music/getdisclist {
            proxy_set_header Host y.qq.com;   // 接口需要请求头
            proxy_set_header Referer https://y.qq.com/portal/playlist.html;
            proxy_pass https://c.y.qq.com/splcloud/fcgi-bin/fcg_get_diss_by_tag.fcg;
    }

浏览器查看Network,可以已经有响应返回,但是控制台报错

Failed to load http://proxy.lushuhao.club/music/getdisclist?..: The 'Access-Control-Allow-Origin' 
header contains multiple values 'http://y.qq.com, *', but only one is allowed. Origin  
'http://localhost:8080' is therefore not allowed access.

是因为QQ音乐的接口设置一个CORS,限制跨域,先前也设置了一个CORS导致重复
现在有一个思路就是,删除调QQ音乐的,留下自己的就能解决

nginx本身只能添加header,想要删除就需要headers-more-nginx-module有详细的安装、使用文档,
安装之前,先看下nginx的编译方式

ubuntu:/usr/local/nginx/conf$ nginx -V

nginx version: nginx/1.13.6
built by gcc 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.4)
built with OpenSSL 1.0.2g  1 Mar 2016
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx  --with-http_ssl_module --with-
http_stub_status_module

将configure arguments: 后面的参数保存下来,原先的安装路径也记住,后面用来覆盖安装

cd /opt // 安装到opt目录
wget http://nginx.org/download/nginx-1.13.6.tar.gz
tar -xzvf nginx-1.13.6.tar.gz
cd nginx-1.13.6/
wget https://github.com/openresty/headers-more-nginx-module/archive/v0.33.tar.gz
tar -xzvf v0.33.tar.gz
./configure --prefix=/usr/local/nginx --add-module=/opt/nginx-1.13.6/headers-more-nginx-module-0.33 --with-http_ssl_module --with-http_stub_status_module
make
mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.old
cp objs/nginx /usr/local/nginx/sbin/nginx 
nginx -v

prefix用先前的,add-module下载解压后的路径,在后面加上先前保存的编译方式

记住,不用make install, 直接用objs下生成的nginx文件替换/usr/local/nginx/sbin/nginx

打开conf文件,在proxy_pass上方加上

more_clear_headers 'Access-Control-Allow-Origin: http://y.qq.com';               

之后重启下nginx服务即可

@mai1me

This comment has been minimized.

Copy link

@mai1me mai1me commented Nov 19, 2019

谢谢 博主,刚好用上。
反代Github Gist 发现遇到Blocked:CSP ,简单编译之后就能用啦。
https://gist.mai1.me

@lushuhao

This comment has been minimized.

Copy link
Owner Author

@lushuhao lushuhao commented Nov 19, 2019

谢谢 博主,刚好用上。
反代Github Gist 发现遇到Blocked:CSP ,简单编译之后就能用啦。
https://gist.mai1.me

共同进步

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.