我必须使用iframe将三个不同的网站嵌入到我的www.myapp.com
应用中。假设这些网站的网址是:
这些网站无法由iframe直接嵌入,因为它们的服务器将X-Frame-Options
响应标头设置为SAMEORIGIN
,因此我使用nginx作为代理来删除这些X-Frame-Options
标头:
# parent application: myapp.com:
server {
listen 8080;
server_name myapp.com www.myapp.com;
location /stand {
root /srv/www;
try_files $uri $uri/ /stand/index.html;
}
}
# website1.com proxy:
server {
listen 8080;
server_name proxywebsite1.com www.proxywebsite1.com;
location / {
proxy_pass https://www.website1.com/;
proxy_set_header www.website1.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_hide_header 'x-frame-options';
}
}
# website2.com proxy:
server {
listen 8080;
server_name proxywebsite2.com www.proxywebsite2.com;
location / {
proxy_pass https://www.website2.com/;
proxy_set_header www.website2.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_hide_header 'x-frame-options';
}
}
# website3.com proxy:
server {
listen 8080;
server_name proxywebsite3.com www.proxywebsite3.com;
location / {
proxy_pass https://www.website3.com/;
proxy_set_header www.website3.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_hide_header 'x-frame-options';
}
}
使用上面的配置,我现在可以将三个网站嵌入到iframe中,即使其域不同(www.myapp.com,proxywebsite1.com,proxywebsite2.com,proxywebsite3.com)。
现在,我想使用JavaScript隐藏嵌入式网站的某些元素(页脚,页眉等),并且由于域不同而被阻止。我在浏览器的控制台中遇到以下错误:
"ERROR DOMException: Blocked a frame with origin xxx from accessing a cross-origin frame"
所以我的问题是:有没有一种配置nginx的方法,以便浏览器不会将myapp.com
和proxywebsite1.com
视为不同的域?