首页 > 解决方案 > 在 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>

标签: laravelapachenginxdocker-compose

解决方案


推荐阅读