Назад | Перейти на главную страницу

Nginx proxy_pass перенаправляет на неправильный URL

Я настроил Nginx / Apache2 / Wordpress на использование кэширования прокси, чтобы предотвратить чрезмерную нагрузку на сервер (дерьмовая тема и плагины).

Все работает, но как только вы нажимаете Wp-Admin, он перенаправляется на http://backend:82 противостоять http://example.com/wp-admin.

Конфиг я взял из: Руководство по Nginx / Apache Proxy_pass.

Я подозреваю, что это правило wp-admin / login, но оно похоже на другие правила Proxy-pass, поэтому я в тупике. Моя конфигурация выглядит следующим образом:

proxy_cache_path /var/run/nginx-cache levels=1:2 keys_zone=main:15m inactive=60m;
#proxy_temp_path /var/run/nginx_cache;

server {
        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;


        server_name ########;

        # Set proxy headers for the passthrough
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        # Let the Set-Cookie header through.
        proxy_pass_header Set-Cookie;

        ## domain.org -> www.domin.org (301 - Permanent)
        if ($host ~* ^([a-z0-9]+\.org)$) {
            set $host_with_www www.$1;
            rewrite ^(.*)$
            http://$host_with_www$1 permanent;
        }

    # Max upload size: make sure this matches the php.ini in .htaccess
        client_max_body_size 8m;

        # Catch the wordpress cookies.
        # Must be set to blank first for when they don't exist.
        set $wordpress_auth "";
        if ($http_cookie ~* "wordpress_logged_in_[^=]*=([^%]+)%7C") {
            set $wordpress_auth wordpress_logged_in_$1;
        }

    # Set the proxy cache key
        set $cache_key $scheme$host$uri$is_args$args;

        # All media (including uploaded) is under wp-content/ so
        # instead of caching the response from apache, we're just
        # going to use nginx to serve directly from there.
        location ~* ^/(wp-content|wp-includes)/(.*)\.(gif|jpg|jpeg|png|ico|bmp|js|css|pdf|doc)$ {
            root /var/www/html/nen;
        }

    # Don't cache these pages.
        location ~* ^/(wp-admin|wp-login.php)
        {
            proxy_pass http://backend;
        }

    location / {
            proxy_pass http://backend;
            proxy_cache main;
            proxy_cache_key $cache_key;
            proxy_cache_valid 30m; # 200, 301 and 302 will be cached.
            # Fallback to stale cache on certain errors.
            # 503 is deliberately missing, if we're down for maintenance
# we want the page to display.
            proxy_cache_use_stale error
                                  timeout
                                  invalid_header
                                  http_500
                                  http_502
                                  http_504
                                  http_404;
            # 2 rules to dedicate the no caching rule for logged in users.
            proxy_cache_bypass $wordpress_auth; # Do not cache the response.
            proxy_no_cache $wordpress_auth; # Do not serve response from cache.
        }

    # Cache purge URL - works in tandem with WP plugin.
        location ~ /purge(/.*) {
            proxy_cache_purge main "$scheme://$host$1";
        }
    } # End server


upstream backend {
        # Defines backends.
        server localhost:82; 
}

Вы должны определить восходящий тег в верхней части сервера. Потому что вы не можете использовать неопределенные переменные в области сервера.

Вот пример кода из вашего источника

upstream backend {
    # Defines backends.
    # Extracting here makes it easier to load balance
    # in the future. Needs to be specific IP as Plesk
    # doesn't have Apache listening on localhost.
    ip_hash;
    server xxx.xxx.xxx.xxx:81; # IP goes here.
}

server {
listen xxx.xxx.xxx.xxx:80; # IP goes here.
    server_name fauna-flora.org www.fauna-flora.org xxx.xxx.xxx.xxx; # IP could go here.

    # Set proxy headers for the passthrough
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;