your programing

NGINX를 Apache에 대한 역방향 프록시로 사용할 때 Wordpress Permalinks가 404를 반환합니다.

lovepro 2023. 6. 11. 21:34
반응형

NGINX를 Apache에 대한 역방향 프록시로 사용할 때 Wordpress Permalinks가 404를 반환합니다.

NGINX가 프록시를 역방향으로 전환하고 포트 8086에서 Apache에서 실행되는 WordPress 사이트에 대한 SSL Termination을 제공하도록 하려고 합니다.나는 NGINX가 정적 파일을 처리하고 PHP 요청만 Apache에 프록시 처리하기를 원합니다.

표준 링크를 사용하여 이 작업을 수행하는 데 성공했습니다(예: https://example.com/ ?post=274가 올바르게 작동함).

모든 종류의 퍼멀 링크를 활성화하면 wp-admin과 마찬가지로 홈 페이지가 로드되지만 https://example.com/what-we-do/ 은 실패합니다.

NGINX 로그를 보니, 그렇군요.

2018/05/23 09:36:40 [error] 7472#0: *1 "/var/www/example.com/live_site/what-we-do/index.php" is not found (2: No such file or directory), client: xxx.xxx.xxx.xxx, server: example.com, request: "GET /what-we-do/ HTTP/2.0", host: "example.com", referrer: "https://example.com/?post=274"

따라서 NGINX는 apache에 전달하는 대신 정적 경로/파일로 /permalink/index.php를 찾으려고 합니다.이걸 어떻게 작동시킬지 생각해 보셨습니까?

내 NGINX 구성은 다음과 같습니다.

upstream example_apache {
    ip_hash;
    server 127.0.0.1:8086;
}

server {
# HTTP/HTTPS Server Block
# General Config
    listen                      [::]:80;
    listen                      80;
    listen                      [::]:443 http2 ssl;
    listen                      443 http2 ssl;
    server_name                 example.com
                                www.example.com;

    root                        /var/www/example.com/live_site;
    access_log                  /var/log/nginx/access-example.com.log main;
    error_log                   /var/log/nginx/error-example.com.log;
    index                       index.php;

#SSL Cert Configuration
# Check SSL config at https://www.ssllabs.com/ssltest/
    ssl_prefer_server_ciphers   on;
    ssl_protocols               TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers                 "ECDHE-ECDSA-CHACHA20-POLY1305 ECDHE-RSA-CHACHA20-POLY1305 EECDH+ECDSA+AESGCM EECDH+aRSA+AESGCM EECDH+ECDSA+SHA384 EECDH+ECDSA+SHA256 EECDH+aRSA+SHA384 EECDH+aRSA+SHA256 EECDH DHE-RSA-CHACHA20-POLY1305 EDH+aRSA !aNULL !eNULL !LOW !3DES !MD5 !EXP !PSK !SRP !DSS !RC4 !SEED !CAMELLIA";
    ssl_session_cache           shared:SSL:100m;
    ssl_session_timeout         180m;
    ssl_dhparam                 /var/www/certs/dh4096.pem;

    ssl_certificate             /var/www/certs/lets_encrypt/web01.example.com/web01.example.com.fullchain.secp384r1.cer;
    ssl_certificate_key         /var/www/certs/lets_encrypt/web01.example.com/web01.example.com.secp384r1.key;
    ssl_certificate             /var/www/certs/lets_encrypt/web01.example.com/web01.example.com.fullchain.rsa4096.cer;
    ssl_certificate_key         /var/www/certs/lets_encrypt/web01.example.com/web01.example.com.rsa4096.key;

# Enable HSTS #Deploy in stages to prevent extended loss to site.
    add_header                  Strict-Transport-Security "max-age=300; includeSubdomains;"; #300s-5min TTL Testing
    #add_header                 Strict-Transport-Security "max-age=604800; includeSubdomains;"; #1week TTL Testing
    #add_header                 Strict-Transport-Security "max-age=2592000; includeSubdomains;"; #1month TTL Testing
    #add_header                 Strict-Transport-Security "max-age=63072000; includeSubDomains; preload"; #10886400s-126days Min for Preload
    #add_header                 Strict-Transport-Security "max-age=63072000; includeSubDomains; preload"; #63072000s-2years Production Value

# OCSP Configuration
    ssl_trusted_certificate     /var/www/certs/lets_encrypt/web01.example.com/web01.example.com.fullchain.secp384r1.cer;
    ssl_stapling                on;
    ssl_stapling_verify         on;
    resolver                    8.8.4.4 8.8.8.8 valid=300s;
    resolver_timeout            10s;

# LetEncrypt webroot alias
    location /.well-known/acme-challenge/ {
        alias /var/www/le_root/.well-known/acme-challenge/;
    }
# www to non-www rewrite
# Redirect to the correct place, if needed
    set $https_redirect 0;
    if ($server_port = 80) { set $https_redirect 1; }
    if ($host ~ '^www\.') { set $https_redirect 1; }
    if ($https_redirect = 1) {
        return 301 https://example.com$request_uri;
    }

# Wordpress entry point
    location / {
        #Try                    file dir    index.php else 404
        try_files               $uri $uri/ /index.php?$args =404;

        #All Files except for *.php
        location ~ .+(?<!\.php)$ {
            location ~ ^[^.]+\.[^.]+$ {
                expires         max;
                add_header      Cache-Control public;
                break;
            }
        }

        #Only *.php files
        location ~ \.php$ {
            proxy_set_header    X-Real-IP           $remote_addr;
            proxy_set_header    Host                $host;
            proxy_set_header    X-Forwarded-For     $proxy_add_x_forwarded_for;
            proxy_set_header    X-Forwarded-Proto   $scheme;
            proxy_pass_header                       Set-Cookie;

            proxy_set_header    SSL_PROTOCOL        $ssl_protocol;
            proxy_set_header    SSL_CLIENT_CERT     $ssl_client_cert;
            proxy_set_header    SSL_CLIENT_VERIFY   $ssl_client_verify;
            proxy_set_header    SSL_SERVER_S_DN     $ssl_client_s_dn;

            proxy_pass                              http://example_apache;
        }
    }
}

이 문제는 프록시 패스 부분에도 도달하지 못하고 있으며 엄격하게 NGINX와 관련된 것으로 보이기 때문에 (내가 알기로는) 다음 사항이 적용되지 않습니다.하지만 누군가는 궁금해 할 것입니다. 그렇지 않으면 다른 사람들이 아파치 구성 측면을 알 수 있도록 이 질문을 비틀거리는 데 도움이 될 수도 있습니다.

내 apache에는 다음이 포함된 .htaccess 파일이 있습니다.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

그리고 나의 wp-config.php의 특징:

// If WordPress is behind reverse proxy
// which proxies https to http
if ( (!empty( $_SERVER['HTTP_X_FORWARDED_HOST'])) ||
 (!empty( $_SERVER['HTTP_X_FORWARDED_FOR'])) ) {

$_SERVER['HTTP_HOST'] = $_SERVER['HTTP_X_FORWARDED_HOST'];

$_SERVER['HTTPS'] = 'on';
}

그리고 내 아파치 구성은 다음과 같습니다.

<VirtualHost *:8086>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/example.com/live_site
ServerName  example.com
ServerAlias www.example.com


ErrorLog ${APACHE_LOG_DIR}/example.com.error.log
CustomLog ${APACHE_LOG_DIR}/example.com.access.log combined

Alias "/.well-known/acme-challenge/" "/var/www/le_root/.well-known/acme-challenge/"

<Directory />
    Options FollowSymLinks
    AllowOverride None
</Directory>
<Directory /var/www/example.com/live_site>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Order allow,deny
    allow from all
</Directory>

</VirtualHost>

또한 Apache에 직접 연결하면 모든 페이지 퍼멀 링크가 올바르게 표시됩니다. (예: http://127.0.0.1:8086/what-we-do/ 올바르게 작동함)

NGINX 제1.13.9절
Apache 2.4.33 mpm_prefork
PHP § 7.1

NGINX가 퍼멀 링크를 아파치에 올바르게 프록시할 수 있도록 도와주시면 감사하겠습니다!

이 명령을 사용하여 mod_rewrite를 사용하도록 설정하거나 확인합니다.

sudo2 enmod 다시 쓰기

NGINX를 사용하여 Prod 환경을 Docker로 마이그레이션할 때 동일한 오류가 발생했지만 Apache에 대해 역방향 프록시를 사용하지 않았습니다.하지만 저의 실수는 똑같았습니다.

그 이유는 제가 그것을 바꿔야 했기 때문입니다.wp_optionsURL에 하도록 합니다.

SELECT * FROM wp_options WHERE option_name='siteurl' OR option_name='home';WordPress Config가 탐색하려는 현재 URL을 표시합니다.그러나 프록시를 만들고 이제 다른 포트나 URL 뒤에 WordPress 사이트가 있으므로 이러한 값을 변경해야 할 수 있습니다.

이 명령을 실행하면 사이트에서 접두사로 사용 중인 두 URL 목록을 받게 됩니다.프록시의 URL이 표시되어 있으면 작동하지 않을 수 있습니다.

그런 다음 새 백엔드 URL + 포트의 위치와 일치하도록 URL을 수정했습니다.이 경우 프록시 자체의 URL이 아니라 프록시 뒤에 있는 포트 및 URL과 일치하도록 변경해야 할 수 있습니다.

내 내부에서 이 값 수정wp-config.php작동하지 않음(예:

 define('WP_HOME','http://local.www.greenhousetreatment.com:8080');
 define('WP_SITEURL','http://local.www.greenhousetreatment.com:8080');

이건 제게 효과가 없었어요

SQL에서 위의 명령을 수동으로 사용한 다음, 해당 값을 PORT와 웹 사이트의 URL에 모두 일치하도록 업데이트해야 했습니다.일반적으로 역방향 프록시에서는 프록시 URL을 입력하면 서비스 IP 및 포트에 도달합니다.서비스 IP와 PORT는 프록시에 대해 전혀 관심이 없기 때문에 필요한 작업을 수행합니다.그것은 심지어 대리인에 대해서도 알지 못합니다.

정말입니까?wp_options프록시 URL이 아닌 실제 서비스 URL 및 포트와 일치합니까?

이것이 빛을 발할 수 있기를 바랍니다.

저도 오늘 같은 문제를 겪고 있습니다. 그리고 제가 이 문제를 해결하기 위해 무엇을 했는지도 당신과 마찬가지입니다.

nginx 구성을 수정하고 다음을 추가/이동합니다.index index.php서버 블록에서 로location /블록으로 막다

다음은 제가 언급한 예입니다.index index.php서버 블록에서 위치 블록에 추가합니다.

server {
   ...
   root                        /var/www/example.com/live_site;
   access_log                  /var/log/nginx/access-example.com.log main;
   error_log                   /var/log/nginx/error-example.com.log;
   #index                       index.php; ##REMOVE THIS ONE 
   ...
# Wordpress entry point
     location / {
        index index.php index.html ##THEN ADD HERE
        #Try                    file dir    index.php else 404
         try_files               $uri $uri/ /index.php?$args =404;
       ...
}

또한 확인하는 것을 잊지 마세요..htaccessWordPress 퍼멀링크용인지 확인하거나, 워드프레스 설정으로 이동한 후 퍼멀링크 설정을 게시 이름으로 저장할 수 있습니다.

언급URL : https://stackoverflow.com/questions/50491693/wordpress-permalinks-return-404-when-using-nginx-as-a-reverse-proxy-to-apache

반응형