
特定の場所に永続的に存在する Docker コンテナ上の複数のアプリを使用して、nginx をリバース プロキシとして設定したいと思います。例:
https://sub.example.com/wiki
https://sub.example.com/app1
https://sub.example.com/app2
特に、mediawiki docker イメージ他のアプリの横に。これが私のdocker-compose.ymlです:
version: '3.5'
services:
mediawiki:
image: mediawiki
restart: unless-stopped
hostname: mediawiki
ports:
- "8080:80"
links:
- database
volumes:
- images:/var/www/html/images
# - ./wiki/LocalSettings.php:/var/www/html/LocalSettings.php
networks:
- wiki
database:
image: mariadb
restart: unless-stopped
hostname: database
environment:
MYSQL_DATABASE: my_wiki
MYSQL_USER: wikiuser
MYSQL_PASSWORD: example
MYSQL_RANDOM_ROOT_PASSWORD: 'yes'
volumes:
- db:/var/lib/mysql
networks:
- wiki
app1:
# ...
expose:
- "4000"
networks:
- apps
nginx:
image: nginx:1.23-alpine
restart: unless-stopped
ports:
- "80:80"
- "443:443"
volumes:
- ./nginx/conf.d:/etc/nginx/conf.d
- ./certbot/conf:/etc/nginx/ssl
- ./certbot/data:/usr/share/nginx/html/letsencrypt
depends_on:
- app1
- mediawiki
networks:
- apps
- wiki
certbot:
image: certbot/certbot:latest
# ...
volumes:
- ./certbot/conf:/etc/letsencrypt
- ./certbot/logs:/var/log/letsencrypt
- ./certbot/data:/usr/share/nginx/html/letsencrypt
networks:
- apps
- wiki
networks:
apps:
wiki:
私が直面している問題は、次のようにするとdefault.conf
、mediawiki コンテナーと他のアプリをプロキシできるものの、特定のリンクとリソースが 404 を返すことです。
upstream testwiki {
server mediawiki:80;
}
server {
listen 80;
listen [::]:80;
server_name sub.example.com;
location / {
return 301 https://$server_name$request_uri;
}
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name sub.example.com;
ssl_certificate /etc/nginx/ssl/live/sub.example.com/fullchain.pem;
ssl_certificate_key /etc/nginx/ssl/live/sub.example.com/privkey.pem;
location /wiki/ {
proxy_pass http://testwiki/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
}
}
この動作の原因は、 などのリレーショナル URL が、ネストされた場所ではなくルートの場所にリクエストを送信するためだと思われます。 (正規表現を含む)、、<a href="/mw-config/index.php">complete the installation</a>
メソッドなど、さまざまなことを試しましたが、最も良い方法は次のとおりです。rewrite
sub_filter
proxy_redirect
proxy_set_header
location /wiki/ {
proxy_pass http://mediawiki:80/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
}
location /app1/ {
proxy_pass http://app1:4000/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
}
if ($http_referer = "https://sub.example.com/wiki/") {
set $proxypass http://mediawiki:80;
}
if ($http_referer = "https://sub.example.com/app1/") {
set $proxypass http://app1:4000;
}
location / {
proxy_pass $proxypass;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
私はまだ mediawiki のベース URL を設定できないため、 に応じてルートの場所で着信要求をプロキシしようとしました。これは、 で行われたすべての初期 GET 要求と、上記のリンクに対してhttp_referer
機能します。https://sub.example.com/wiki/
ただし、 をクリックした後<a href="/mw-config/index.php">...
、index.php
はさらにリクエストを行い、再び にリダイレクトされますhttps://sub.example.com/
。URL は書き換えられず、リファラーが を示しているためhttps://sub.example.com/mw-config/index.php
、これらのリクエストは 500 を返します。
私の質問は、この動作を修正して、アプリがそれぞれの場所に永続的に存在するようにするにはどうすればよいかということです。残念ながら、現時点ではサブドメインを変更することはできません。
どのような助けでも大歓迎です!
編集:
同様の問題が発生する可能性のある他の複数のアプリを使用したいので、より一般的な解決策を考えたいと思います。場合によっては、ベース URL を制御できません。
答え1
これを機能させるには、ここでいくつかの事柄について同じ認識を持つ必要があります。
- NGINX とは構成: パスベースルーティング、リバースプロキシ
- あなたの設定を試してみましたが、あなたの設定は正しいと思います
- アパッチ2設定: URIをファイルパスとスクリプトに正しくマッピングする
- ディレクティブを使用して、内部ファイルパスを正しく
AliasMatch "^/wiki(.*)" "/var/www/html/$1"
削除してマップします。/wiki
- ディレクティブを使用して、内部ファイルパスを正しく
- メディアウィキ設定: パスを追加して、リンクをリバース プロキシする場所を認識できる
/wiki
ようにし、を削除して正しい内部ファイル パスを使用できるようにします。Nginx
Apache2
/wiki
- MediaWikiのセットアップを実行した後に作成されたファイル
$wgScriptPath = "/wiki";
で設定し、LocalSettings.php
docker-compose.yml
- MediaWikiのセットアップを実行した後に作成されたファイル
- Docker の作成
mediawiki
: 変更したローカルファイルをDockerコンテナにマウントして変更を永続化します。
アパッチ2
AliasMatch "^/wiki(.*)" "/var/www/html/$1"
の設定に追加します/etc/apache2/sites-enabled/000-default.conf
。また、このファイルの内容を上書きする必要があります。docker-compose.yml
#/etc/apache2/sites-enabled/000-default.conf
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
AliasMatch "^/wiki(.*)" "/var/www/html/$1"
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
AllowEncodedSlashes NoDecode
</VirtualHost>
MediaWiki 設定:
これは変更が必要なファイルのほんの一部ですLocalSettings.php
。ここにデフォルト全体を含めるには少し大きいです。docker LocalSettings.php
-compose を使用して docker コンテナにマウントしているローカル コピーが既にあるようですので、そこで変数を変更して$wgScriptPath
コンテナを再起動してください。
## The URL base path to the directory containing the wiki;
## defaults for all runtime URL paths are based off of this.
## For more information on customizing the URLs
## (like /w/index.php/Page_title to /wiki/Page_title) please see:
## https://www.mediawiki.org/wiki/Manual:Short_URL
$wgScriptPath = "/wiki";
Docker の作成
今ボリュームセクションを修正しますdocker-compose.ymlのMediaWiki部分をapache2の設定を上書きするMediaWiki サイトのファイル。
mediawiki:
image: mediawiki
restart: unless-stopped
hostname: mediawiki
ports:
- "8080:80"
links:
- database
volumes:
- ./images:/var/www/html/images
- ./wiki/LocalSettings.php:/var/www/html/LocalSettings.php
- ./apache2/000-default.conf:/etc/apache2/sites-enabled/000-default.conf
networks:
- wiki
答え2
この問題に対する唯一の安定した解決策は、ルート URL を設定することです。
「mediawikiルートURL」を検索したところ、2番目にヒットしたのはhttps://www.mediawiki.org/wiki/Topic:Ry90289pdqa86yrd最初の応答では、MediaWiki でルート URL を設定する方法が示されています。
Wiki の URL は LocalSettings.php で設定する必要があります ($wgLogo も同様で、これもこのファイルで設定されます)。また、$wgServer を別の値に設定することで変更できます。これにより、MediaWiki は必要なドメインで始まる URL を出力するようになります。