如何为Phoenix应用程序设置Prerender

时间:2018-04-22 00:22:29

标签: angular nginx elixir phoenix-framework prerender

我们在localhost:3000上运行Prerender,我们的Web应用程序在localhost:39499上运行。我们网站的问题是Google无法在我们的Phoenix应用程序中看到Angular.js中运行的javascript。我们的问题是Pheonix在nginx中运行代理从域名到localhost上的端口,这阻止我们在nginx上运行代理到服务器上托管Prerender的端口。我们希望nginx有一个嵌套的代理设置,所以我们转到端口39499然后再到端口3000(如果可能的话)。有没有办法在两台服务都运行的服务器上执行此操作?

以下是我们在启用网站的nginx配置文件:

# -*- mode: nginx -*-
upstream broad_highway {
    server 127.0.0.1:39499;
}

server {
    if ($host = meetings.aahmbny.org) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen 80;
    listen [::]:80;
    server_name meetings.aahmbny.org;

    location / {
        return 301 https://meetings.aahmbny.org$request_uri;
    }

    # for LetsEncrypt
    location '/.well-known/acme-challenge' {
        default_type "text/plain";
        root /tmp/letsencrypt-auto;
    }


}

server {
    listen 80;
    listen [::]:80;
    server_name beta.meetings.aahmbny.org;

    location / {
        return 301 https://beta.meetings.aahmbny.org$request_uri;
    }

    # for LetsEncrypt
    location '/.well-known/acme-challenge' {
        default_type "text/plain";
        root /tmp/letsencrypt-auto;
    }
}

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    server_name meetings.aahmbny.org;
    gzip on;
    gzip_proxied any;
    ssl_certificate /etc/letsencrypt/live/meetings.aahmbny.org/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/meetings.aahmbny.org/privkey.pem; # managed by Certbot
    ssl_trusted_certificate /etc/letsencrypt/live/meetings.aahmbny.org/fullchain.pem;

    add_header X-Frame-Options DENY;
    add_header X-Content-Type-Options nosniff;

    location = /index.html {
        rewrite  ^ / permanent;
        try_files /index.html =404;
    }

    location / {
        try_files $uri @proxy;
    }

    location /sitemaps {
        alias /home/broad-highway/sitemaps/;
    }

    location @proxy {
        proxy_redirect off;
        proxy_pass http://broad_highway;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host localhost:3000$http_host;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }

    access_log /var/log/nginx/broad-highway-access.log;
    error_log /var/log/nginx/broad-highway-error.log;

}

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    server_name beta.meetings.aahmbny.org;
    gzip on;
    gzip_proxied any;
    ssl_certificate /etc/letsencrypt/live/beta.meetings.aahmbny.org/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/beta.meetings.aahmbny.org/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/beta.meetings.aahmbny.org/fullchain.pem;

    add_header X-Frame-Options DENY;
    add_header X-Content-Type-Options nosniff;

    location / {
        try_files $uri @proxy;
    }

    location /sitemaps {
        alias /home/broad-highway/sitemaps/;
    }

    location @proxy {
        proxy_redirect off;
        proxy_pass http://broad_highway;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }

    access_log /var/log/nginx/broad-highway-access.log;
    error_log /var/log/nginx/broad-highway-error.log;
}

1 个答案:

答案 0 :(得分:0)

您可以尝试这样的方法,以便将proxy_pass发送到您的预渲染服务器以获取爬虫,或者将proxy_pass发送到您的普通服务器以供用户使用:

location @proxy {
    proxy_redirect off;

    proxy_set_header X-Prerender-Token YOUR_TOKEN;

    set $prerender 0;
    if ($http_user_agent ~* "baiduspider|twitterbot|facebookexternalhit|rogerbot|linkedinbot|embedly|quora link preview|showyoubot|outbrain|pinterest|slackbot|vkShare|W3C_Validator") {
        set $prerender 1;
    }
    if ($args ~ "_escaped_fragment_") {
        set $prerender 1;
    }
    if ($http_user_agent ~ "Prerender") {
        set $prerender 0;
    }
    if ($uri ~* "\.(js|css|xml|less|png|jpg|jpeg|gif|pdf|doc|txt|ico|rss|zip|mp3|rar|exe|wmv|doc|avi|ppt|mpg|mpeg|tif|wav|mov|psd|ai|xls|mp4|m4a|swf|dat|dmg|iso|flv|m4v|torrent|ttf|woff|svg|eot)") {
        set $prerender 0;
    }

    #resolve using Google's DNS server to force DNS resolution and prevent caching of IPs
    resolver 8.8.8.8;

    if ($prerender = 1) {

        #setting prerender as a variable forces DNS resolution since nginx caches IPs and doesnt play well with load balancing
        set $prerender "service.prerender.io";
        rewrite .* /$scheme://$host$request_uri? break;
        proxy_pass http://$prerender;
    }

    proxy_pass http://broad_highway;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host localhost:3000$http_host;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
}