laravel - 在 apache 后面的 docker-compose 上带有 nginx 的 Laravel 应用程序重定向到 nginx 域而不是 Apache 域
问题描述
我有一个运行 docker-compose 堆栈的 ubuntu 服务器,其中包括 nginx 作为暴露端口 8081 的 http-server,并且在主机上,apache2 正在将 site.com 请求重定向到 site.local:8081。
例如,laravel 应用程序在登录后重定向到 site.local:8081/home,而不是我希望它重定向到 site.com/home。而且我不知道这将通过 laravel 或 nginx 解决;
而且我不知道应用程序的整个结构(Apache 到 Docker 容器中的 nginx)是否是最好的结构。Laravel .env:(为简单起见省略了行)
APP_URL=https://example.com # (example=site)
ASSET_URL=https://example.com # (example=site)
Laravel AppServiceProvider.php:(为简单起见省略了行)
public function boot()
{
if (env('SERVER', 'local') === 'online')
URL::forceScheme('https');
URL::forceRootUrl(env('APP_URL'));
URL::formatHostUsing(function(){
return env('APP_URL');
});
.
.
.
}
Laravel LoginController.php:(已省略行)
protected function authenticated(Request $request, $user)
{
$result = UserLogin::dispatch($user);
if ($result[0] instanceof RedirectResponse) return $result[0];
dd(redirect()->intended($this->redirectPath()),$this->redirectPath());
if ($user->hasRole('newbie') && $user->can('form.upss')) return redirect('/upsss');
if ($user->hasRole('newbie') && $user->can('form.biz')) return redirect('/auditor');
else return redirect()->intended($this->redirectPath());
}
docker-compose.yml:(为简单起见省略了行)
# Services
services:
# Nginx Service
nginx:
# user: "1000:"
build: ./.docker/nginx
ports:
- 8081:80
networks:
default:
aliases:
- site.local
- db.site.local
volumes:
- ./src/backend:/var/www/backend:ro,delegated
- ./.docker/nginx/conf.d:/etc/nginx/conf.d:ro
- phpmyadmindata:/usr/src/phpmyadmin:delegated
- ./.docker/nginx/certs:/etc/nginx/certs:delegated
depends_on:
- backend
# Backend Service
backend:
user: "${UID}:${GID}"
labels:
ofelia.enabled: "true"
build:
context: ./src/backend
target: backend
working_dir: /var/www/backend
volumes:
- ./src/backend:/var/www/backend:delegated
- ./.docker/backend/init.sh:/opt/files/init.sh:delegated,ro
- ./.docker/nginx/certs:/usr/local/share/ca-certificates:delegated,ro
Nginx 后端.conf:
server {
listen 80;
listen [::]:80;
server_name site.local;
root /var/www/backend/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
index index.html index.htm index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~* \.php$ {
fastcgi_pass backend:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
阿帕奇配置:
<VirtualHost *:80>
ServerName site.com
ServerAdmin webmaster@localhost
ErrorLog ${APACHE_LOG_DIR}/site_error.log
CustomLog ${APACHE_LOG_DIR}/site_access.log combined
ProxyPass / http://site.local:8081/
ProxyPassReverse / http://site.local:8081/
<Proxy *>
allow from all
</Proxy>
RewriteEngine on
RewriteCond %{SERVER_NAME} =site.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName site.com
ServerAdmin webmaster@localhost
ErrorLog ${APACHE_LOG_DIR}/site_error.log
CustomLog ${APACHE_LOG_DIR}/site_access.log combined
ProxyPass / http://site.local:8081/
ProxyPassReverse / http://site.local:8081/
<Proxy *>
allow from all
</Proxy>
# Include /etc/letsencrypt/options-ssl-apache.conf
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/site.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/site.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>
解决方案
推荐阅读
- cypress - 在柏树中单击链接时页面未加载
- java - 有没有办法在 Atom 项目中隐藏某些类型的文件
- c# - 如何检查字符串是否包含数据库记录?
- swiftui - 在 HStack 容器中连接两个文本视图?
- label - 在 Automl 对象检测中将 2 个不同标签的所有框合并为一个标签
- android - 如何在加速度计中仅获得水平加速度
- php - 将 Opencart 附属变量名称从 Tracking 更改为 Ref?
- r - 如何在按 x 变量而不是填充变量分组的条形图上绘制 geom_line?
- javascript - 如何让搜索过滤器循环遍历库存项目表中的 MULTIPLE 数据类型,并缩小显示的结果范围?
- python - 在 python 中使用 Windows 上 .so 文件中的函数