1

通过 Amazon API Gateway 和网络负载均衡器使用 Nginx 设置 Wordpress 时面临重定向问题。

描述:-
我们有我们的主要网站 xyz.com(由 Amazon API 网关和负载均衡器提供服务),并希望我们的博客出现在 xyz.com/blogs 上。因此,我们设置了 Amazon API 网关和负载均衡器,以将 xyz.com/blogs 的任何请求重定向到包含带有 Nginx 的 Wordpress 的 EC2。

问题:-
我们面临的问题是,主页渲染得很好,但是当我们尝试渲染任何其他页面时,例如:- xyz.com/blogs/my-first-post/xyz.com/blogs/wp -行政然后它卡在那里,没有任何反应。作为我们初始调试的一部分,我们发现 Wordpress 正在重定向到网络负载均衡器 url,(根据我们的猜测)无法访问,我们没有得到任何响应。

这就是我们默认的 nginx conf 的样子(/etc/nginx/conf.d/xyz_blogs.conf),我们从这个链接 => Wordpress|Nginx

# Upstream to abstract backend connection(s) for php
upstream php {
        server unix:/tmp/php-cgi.socket;
        server 127.0.0.1:9000;
}

server {
        ## Your website name goes here.
        server_name xyz.com;
        ## Your only path reference.
        root /var/www/html;
        ## This should be in your http block and if it is, it's not needed here.
        index index.php;

        location = /favicon.ico {
                log_not_found off;
                access_log off;
        }

        location = /robots.txt {
                allow all;
                log_not_found off;
                access_log off;
        }

        location / {
                # This is cool because no php is touched for static content.
                # include the "?$args" part so non-default permalinks doesn't break when using query string
                try_files $uri $uri/ /index.php?$args;
        }

        location ~ \.php$ {
                #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
                include fastcgi.conf;
                fastcgi_intercept_errors on;
                fastcgi_pass php;
        }

        location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
                expires max;
                log_not_found off;
        }
}

我们将如何解决这个问题?
事先感谢您在此提供的任何帮助。

4

2 回答 2

0

你这个:

upstream php-app { # <-- change name of the upstram
        # server unix:/tmp/php-cgi.socket; # <-- Remove this
        # List of [IP:Port] OR list of [sockets] not both mixed

        server 127.0.0.1:9000;
        # server 127.0.0.1:9001; # example
        # server 127.0.0.1:9002; # example
}

server {
        listen 80 default_server;       # <-- Add this line
        listen [::]:80 default_server;  # <-- Add this line

        server_name xyz.com *.xyz.com;

        root /var/www/html;
        # index index.php;    # <-- Will be removed, you app is provided by the socket/port

        location = /favicon.ico {
                log_not_found off;
                access_log off;
        }

        location = /robots.txt {
                allow all;
                log_not_found off;
                access_log off;
        }

        location ~ \.php$ {
                include fastcgi.conf;
                fastcgi_intercept_errors on;
                fastcgi_pass php;
        }

        location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
                expires max;
                log_not_found off;
        }

        location / { # Put this at the end, NGinx apply priority
                proxy_pass http://php-app; # Add this line
        }
}

更新1:

更新配置

    ...
    server_name xyz.com *.xyz.com *.amazonaws.com;
    ...
于 2020-02-28T08:46:52.533 回答
0

很抱歉,迟到的帖子,但目前我们已经使用可能没有太大可扩展性的解决方案实现了这一点。我们所做的是将我们的网站和 wordpress 部署在同一台机器上但在不同的容器中。因此,我们的网站在不同的容器中运行,而 wordpress 在不同的容器中运行,但两者都存在于同一个 EC2 中。我们在 wordpress 中使用 nginx 配置将请求重定向到同伴容器。目前我的 nginx 配置看起来像这样:

upstream php {
        server unix:/tmp/php-cgi.socket;
        server 127.0.0.1:9000;
}

server {

    listen 80;
    server_name xyz.com;

    root /var/www/html;
    rewrite /wp-admin$ $scheme://$host$uri/ permanent;
    index index.php;

    location /blog  {
        try_files $uri $uri/ /blog/index.php?$args;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass php;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
}
    if (!-e $request_filename) {
        rewrite /wp-admin$ $scheme://$host$uri/ permanent;
    }

    location /  {
            rewrite ^/(.*) /$1 break;
            proxy_pass  http://xyz-container:80;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
    }
}

如果我们能够以某种方式在不同的 EC2 中部署 wordpress 容器并仍然实现这一点,那就太好了。如果有人可以针对不同 EC2 中的 wordpress 容器的问题提出更好的可扩展工作解决方案,那将真的很有帮助,而且会更好。谢谢。

于 2021-06-06T08:51:56.777 回答