簡體   English   中英

在 Nginx 反向代理后面運行 Apache(使用 .htaccess)應用程序

[英]Running Apache (with .htaccess) App Behind Nginx Rerverse Proxy

我最近開始嘗試對我的服務進行 Docker 化,我已經到了將所有已經構建了鏡像的東西進行 Docker 化的地步。 現在我正在嘗試為 facileManager (FM) 構建一個圖像,它還沒有。 我大部分時間都在工作,但在 Nginx 后面運行時遇到問題。FM 通常是一個 apache-php 應用程序,不包括 Nginx 的安裝說明。我注意到我的容器/圖像是當我通過已發布的端口直接連接到它時它工作正常,但如果我嘗試通過 Nginx 連接到它,它會出錯並抱怨 .htaccess 文件不工作。 我不是 Apache 或 Nginx 方面的專家,所以我進行了 Google 搜索,但除了 Wordpress 之外並沒有想出太多關於它的“漂亮網址”的類似問題,所以我希望這里有人可以提供幫助。

首先是應用程序的 Github 存儲庫: https://github.com/WillyXJ/facileManager/tree/ea159f5f6112727de8422c552aa05b6682aa4d79/server

.htaccess文件具體是:

<IfModule mod_headers.c>
    <FilesMatch "\.(js|css|txt)$">
        Header set Cache-Control "max-age=7200"
    </FilesMatch>
    <FilesMatch "\.(jpe?g|png|gif|ico)$">
        Header set Cache-Control "max-age=2592000"
    </FilesMatch>
</IfModule>

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>

我得到的確切錯誤來自這里: https://github.com/WillyXJ/facileManager/blob/ea159f5f6112727de8422c552aa05b6682aa4d79/server/fm-includes/init.php#L153

if (!defined('INSTALL')) {
        if (@dns_get_record($_SERVER['SERVER_NAME'], DNS_A + DNS_AAAA)) {
            $test_output = getPostData($GLOBALS['FM_URL'] . 'admin-accounts.php?verify', array('module_type' => 'CLIENT'));
            $test_output = isSerialized($test_output) ? unserialize($test_output) : $test_output;
            if (strpos($test_output, 'Account is not found.') === false) {
                $message = sprintf(_('The required .htaccess file appears to not work with your Apache configuration which is required by %1s. '
                        . 'AllowOverride None in your configuration may be blocking the use of .htaccess or %s is not resolvable.'),
                        $fm_name, $_SERVER['SERVER_NAME']);
                if ($single_check) {
                    bailOut($message);
                } else {
                    $requirement_check .= displayProgress(_('Test Rewrites'), false, 'display', $message);
                    $error = true;
                }
            } else {
                if (!$single_check) $requirement_check .= displayProgress(_('Test Rewrites'), true, 'display');
            }
        }
    }

Nginx 配置:

server {
        listen 80;

        server_tokens off;

        location /.well-known/acme-challenge/ {
                root /var/www/certbot;
        }

        location / {
                return 301 https://$host$request_uri;
        }
}

server {
        listen 443 ssl;

        server_name bound.example.com;

        ssl_certificate /etc/nginx/ssl/live/bound.example.com/fullchain.pem;
        ssl_certificate_key /etc/nginx/ssl/live/bound.example.com/privkey.pem;
        include /etc/nginx/ssl/options-ssl-nginx.conf;
        ssl_dhparam /etc/nginx/ssl/ssl-dhparams.pem;

        location / {
                proxy_pass http://FM.;     <<< Docker service name
                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-Forward-For $proxy_add_x_forwarded_for;
        }
}

我的 Dockerfile/命令運行: https://github.com/MeCJay12/facileManager-docker

FROM php:7.4-apache

ENV TZ=UTC
ENV Version=4.2.0
ARG DEBIAN_FRONTEND=noninteractive
WORKDIR /src

RUN apt-get update \
    && apt-get -qqy install wget libldb-dev libldap2-dev tzdata \
    && wget http://www.facilemanager.com/download/facilemanager-complete-$Version.tar.gz \
    && tar -xvf facilemanager-complete-$Version.tar.gz \
    && mv facileManager/server/* /var/www/html/

RUN ln -s /usr/lib/x86_64-linux-gnu/libldap.so /usr/lib/libldap.so \
    && docker-php-ext-install mysqli ldap \
    && a2enmod rewrite dump_io

COPY php.ini /usr/local/etc/php/php.ini
RUN rm -r /src

獎勵問題:直接通過已發布的端口訪問此容器/圖像時,圖像全部損壞。 我認為它是相關的,因為 .htaccess 文件包含對圖像文件的引用。

先謝謝您的幫助!

https://github.com/WillyXJ/facileManager/issues/491中找到解決方案。 需要刪除 webapp 中工作不正常的支票。

下面是我用來反向代理到 Ubiquiti Unifi 容器的配置文件。 我所有的 certbot 都是在場外處理的,所以我不需要在這里考慮。 如果您比較我們的位置塊,問題可能會立即顯現出來,但為了清楚起見,我會解釋一下。

首先,我只想指出我已經包含了一個解析器。 IP 地址 127.0.0.11 指向Docker 內部的 DNS 解析器。 您可能不需要包含此內容,但是我自己這樣做是為了確保我沒有遇到奇怪的問題。

其次,代理通行證。 這當然是魔法發生的地方。 我注意到您沒有包含 $request_uri,因此 nginx 收到的任何針對bound.example.com/testpage1的請求都會向上游 apache 服務器發送一個請求,以獲取bound.example.com 當然,如果您需要包括一個端口,就像我在這里所做的8443一樣,這也是這樣做的地方。

最后,我想建議您考慮升級您的 SSL 設置,以確保您免受來自較弱 SSL / TLS 版本的攻擊。

我希望將變量 $request_uri 添加到您的代理通行證指令是使您的站點正常運行所需的全部內容。

server {
    listen       80;
    server_name  unifi.localdomain;

    location / {
        return 301 https://$host$request_uri;
    }
}

server {
    listen                     443 ssl;
    server_name                unifi.localdomain;

    add_header                 Strict-Transport-Security "max-age=31536000; includeSubDomains; preload;";

    ssl_protocols              TLSv1.3 TLSv1.2;
    ssl_session_cache          builtin:1000  shared:SSL:10m;
    ssl_session_timeout        1d;
    ssl_ciphers                ALL:!RSA:!CAMELLIA:!aNULL:!eNULL:!LOW:!3DES:!MD5:!EXP:!PSK:!SRP:!DSS:!RC4:!SHA1:!SHA256:!SHA384;
    ssl_prefer_server_ciphers  on;
    ssl_stapling               on;
    ssl_stapling_verify        on;
    resolver                   8.8.8.8 8.8.4.4 valid=300s;
    resolver_timeout           5s;
    ssl_certificate            /etc/nginx/certs/fullchain.pem;
    ssl_certificate_key        /etc/nginx/certs/privkey.pem;
    ssl_dhparam                /etc/nginx/certs/dhparam.pem;

    location / {
        resolver          127.0.0.11;
        proxy_pass        https://unifi:8443$request_uri;
        proxy_set_header  X-Real-IP  $remote_addr;
        proxy_set_header  X-Forwarded-For $remote_addr;
        proxy_set_header  Host $host;
        proxy_set_header  Upgrade $http_upgrade;
        proxy_set_header  Connection "Upgrade";
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM