简体   繁体   English

当Nginx反向代理到Node.js时,Safari中的Lazyload无法正常工作

[英]Lazyload in Safari not working when Nginx reverse proxy to node.js

Here is the problem: I have a node.js server which is behind a Nginx proxy. 问题出在这里:我有一个位于Nginx代理后面的node.js服务器。 Nginx is configured to serve the static contents and proxy_pass others to node.js Also, in the node app, the images are loaded by a lazyloading solution. Nginx被配置为提供静态内容,并将proxy_pass传递给node.js。此外,在节点应用程序中,图像是通过惰性加载解决方案加载的。

In Chrome, when I access my app (example.com), everything works as it should. 在Chrome浏览器中,当我访问我的应用程序(example.com)时,一切正常。 The lazyload works fine and images are loaded and served by nginx. lazyload可以正常工作,图像由nginx加载并提供服务。

In Safari, my app (example.com) loads fine (which means that node.js server and nginx proxy are working). 在Safari中,我的应用程序(example.com)可以正常加载(这意味着node.js服务器和nginx代理可以正常工作)。 But images are not loaded ! 但是图像未加载! It seams the request from lazyload are not sent or did not get any response. 它表明来自lazyload的请求未发送或未获得任何响应。

If I enter the images' uri directly in Safari, they loads fine. 如果我直接在Safari中输入图片的uri,则可以正常加载。

I should mention that when I locally use node.js server (without nginx) there is no problem even in Safari. 我应该提到,当我在本地使用node.js服务器(不使用nginx)时,即使在Safari中也没有问题。

So It seems there is problem between lazyload, Nginx, Node.js and Safari, as everything is ok in Chrome. 因此,由于Chrome一切正常,因此lazyload,Nginx,Node.js和Safari之间似乎存在问题。

Below you find my nginx.conf: 在下面找到我的nginx.conf:

http {
   log_format  main  ‘$remote_addr - $remote_user [$time_local] “$request” ’
                     ‘$status $body_bytes_sent “$http_referer” ’
                     ‘“$http_user_agent” “$http_x_forwarded_for”’;
   access_log  /var/log/nginx/access.log  main;

   sendfile            on;
   tcp_nopush          on;
   tcp_nodelay         on;
   keepalive_timeout   65;
   types_hash_max_size 2048;

   include             /etc/nginx/mime.types;
   default_type        application/octet-stream;

   server {

    server_name  example.com www.example.com;

        access_log /var/log/nginx/example.com/nginx.access.log;
        error_log  /var/log/nginx/example.com/nginx.error.log;

    location /img/ {
       root          /var/nginx/html;
    }

    location / {

        proxy_pass http://127.0.0.1:3000; #nodejs server
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;

        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 https;
     }

    error_page 404 /404.html;
            location = /40x.html {
        }

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

I would first try removing these if you're also using X-Forwarded-Proto 如果您还使用X-Forwarded-Proto我会先尝试将其删除

proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';

This allows you to terminate the SSL with the proxy, while speaking plain http downstream, therefore you should not need to upgrade the connection. 这使您可以在下游使用纯http时终止与代理的SSL,因此您无需升级连接。

I just found the answer to my question myself. 我自己才找到问题的答案。 I post here in case it could be helpful for others. 我在这里发布,以免对其他人有帮助。

I noticed in Safari console that it is blocking images from loading as their uri begin with http not https (insecure content over https), so they are not loaded But Chrome shows insecure contents over https, so images are loaded. 我在Safari控制台中注意到,由于其uri以http而不是https(https上的不安全内容)开头,因此它阻止了图像的加载,因此未加载它们,但是Chrome显示了https上的不安全内容,因此图像已加载。 It just shows a warning beside the url field. 它只是在url字段旁边显示警告。

So, it has nothing to do with node.js or nginx reverse proxy :) 因此,它与node.js或nginx反向代理无关:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM