Laravel + Vite + Sail over HTTPS,有可能?

3pmvbmvn  于 2023-05-30  发布在  其他
关注(0)|答案(3)|浏览(175)

经过几个小时的尝试,我想我太困惑了,不明白出了什么问题...标题很好地解释了我想做的工作。我的docker-compose.yml

version: '3'
services:
    mysite.test:
        build:
            context: ./docker/8.1
            dockerfile: Dockerfile
            args:
                WWWGROUP: '${WWWGROUP}'
        image: sail-8.1/app
        extra_hosts:
            - 'host.docker.internal:host-gateway'
        ports:
            - '${APP_PORT:-80}:80'
            - '443:443' //added for test but not working...
            - '${HMR_PORT:-8080}:8080'
            - '5173:5173' //Vite port
        environment:
            WWWUSER: '${WWWUSER}'
            LARAVEL_SAIL: 1
            XDEBUG_MODE: '${SAIL_XDEBUG_MODE:-off}'
            XDEBUG_CONFIG: '${SAIL_XDEBUG_CONFIG:-client_host=host.docker.internal}'
        volumes:
            - '.:/var/www/html'
        networks:
            - sail
        depends_on:
            - mysql
            - minio
    mysql:
        image: 'mysql/mysql-server:8.0'
        ports:
            - '${FORWARD_DB_PORT:-3306}:3306'
        environment:
            MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
            MYSQL_ROOT_HOST: "%"
            MYSQL_DATABASE: '${DB_DATABASE}'
            MYSQL_USER: '${DB_USERNAME}'
            MYSQL_PASSWORD: '${DB_PASSWORD}'
            MYSQL_ALLOW_EMPTY_PASSWORD: 1
        volumes:
            - 'sail-mysql:/var/lib/mysql'
            - './vendor/laravel/sail/database/mysql/create-testing-database.sh:/docker-entrypoint-initdb.d/10-create-testing-database.sh'
        networks:
            - sail
        healthcheck:
            test: ["CMD", "mysqladmin", "ping", "-p${DB_PASSWORD}"]
            retries: 3
            timeout: 5s
networks:
    sail:
        driver: bridge
volumes:
    sail-nginx:
        driver: local
    sail-mysql:
        driver: local

我的vite.config.js

import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';

export default defineConfig({
    server: {
        https: true,
        host: '0.0.0.0'
    },
    plugins: [
        laravel({
            input: [
                'resources/css/app.css',
                'resources/js/app.js',
            ],
            refresh: true
        }),
    ],
});

我的.env

(...)
APP_URL=https://mysite.test
APP_SERVICE=mysite.test
(...)

该配置的结果是它可以在http://mysite.test中工作,但不能在https中工作。该返回:

无法访问此站点
mysite.test意外关闭连接。

有人能给我点建议吗?谢谢🙏你!

iezvtpos

iezvtpos1#

我设法让这个工作与球童使用以下要点https://gist.github.com/gilbitron/36d48eb751875bebdf43da0a91c9faec
完成以上所有操作后,我将vite端口添加到laravel.test服务下的docker-compose.yml

ports:
    - '5173:5173'

另外vite本身也需要一个ssl证书
vite.config.js

import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
import vue from '@vitejs/plugin-vue';
import basicSsl from '@vitejs/plugin-basic-ssl'

export default defineConfig({
    server: {
        https: true,
        host: '0.0.0.0',
        hmr: {
            host: 'localhost'
        },
    },
    plugins: [
        basicSsl(),
        laravel({
            input: 'resources/js/app.js',
            refresh: true,
        }),
        vue({
            template: {
                transformAssetUrls: {
                    base: null,
                    includeAbsolute: false,
                },
            },
        }),
    ],
});

由于某种原因,我的应用程序生成的是http链接,而不是https。因此,我将以下内容添加到AppServiceProvider.phpboot方法中

\URL::forceScheme('https');
sxissh06

sxissh062#

使用share命令可以通过expose运行https隧道。您的域名将看起来像
https://[something]. expose.dev
获得真实的的全球可信SSL证书只能由证书颁发机构完成,例如Let's Encrypt。他们需要通过http质询或dns质询来验证您拥有该域。
由于您无法真正拥有.test域名,因此您唯一的选择是sign a certificate yourself,并将其添加到您自己的计算机上以及它的根证书。如果你这样做,只有你的计算机将显示连接为安全的。

zte4gxcn

zte4gxcn3#

对于https中的加载资源@vite(['resources/sass/app.scss', 'resources/js/app.js']):在.env中添加这一行:ASSET_URL=https://midomain.com

相关问题