我在将 nginx 与 traefik 一起使用时遇到了问题。我的项目可以使用 nginx 镜像独立正常工作,但是当我尝试实现 traefik 时,我遇到了一些麻烦,我的 SERVER 全局变量不包含 HTTPS 密钥,关于这个 laravel 不理解它并用于 assetic 和其他使用的东西
\Illuminate\Routing\UrlGenerator::formatScheme
- http
代替 https
。当我尝试提供强制模式解决方案时,这已解决问题,但我想这对于这种情况来说不是正确的方法,因为独立的一切都很完美。所以这是我的独立版本的 nginx,它可以正常工作并且使用 https 模式加载资产:
nginx:
build:
context: .
dockerfile: ./nginx/Dockerfile
restart: on-failure
volumes:
- "../:/var/www"
ports:
- "80:80"
- "443:443"
与会议
server {
server_name my_project.local.com;
listen 443 ssl;
ssl_certificate /etc/nginx/certificates/cert.pem;
ssl_certificate_key /etc/nginx/certificates/key.pem;
root /var/www/public;
index index.php index.html index.htm;
location /storage/ {
alias /var/www/storage/app/public/;
autoindex off;
}
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
try_files $uri /index.php =404;
client_max_body_size 50m;
fastcgi_pass php-img:9000;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
fastcgi_read_timeout 3600;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /var/www/public/index.php;
fastcgi_param PHP_VALUE "error_log=/var/log/nginx/php-img_errors.log";
}
access_log /var/log/nginx/php-img_service.access.log;
error_log /var/log/nginx/php-img_service.error.log crit;
location ~ /\.ht {
deny all;
}
}
以及我添加 traefic 图像时的另一种情况
lu-traefik:
image: traefik:2.4.8
ports:
# The HTTP port
- "80:80"
- "443:443"
- "4200:4200"
# The Web UI (enabled by --api.insecure=true)
- "8080:8080"
volumes:
# So that Traefik can listen to the Docker events
- /var/run/docker.sock:/var/run/docker.sock
- type: bind
source: ./docker/lu-traefik/dev/traefik.yml
target: /etc/traefik/traefik.yml
- type: bind
source: ./docker/lu-traefik/dev/configuration/
target: /traefik/configuration/
- type: bind
source: ./docker/lu-traefik/dev/tls
target: /traefik/tls
logging:
options:
max-size: "10m"
max-file: "5"
lu-backend-nginx:
image: nginx:1.21.0-alpine
depends_on:
- lu-backend-php
- lu-traefik
restart: on-failure
labels:
- traefik.enable=true
- traefik.http.routers.r-lu-backend-nginx-https.rule=Host(`lu-backend.local.com`)
- traefik.http.routers.r-lu-backend-nginx-https.entrypoints=web-secure
- traefik.http.routers.r-lu-backend-nginx-https.service=s-lu-backend-nginx-https
- traefik.http.routers.r-lu-backend-nginx-https.tls=true
- traefik.http.services.s-lu-backend-nginx-https.loadbalancer.server.port=80
expose:
- "80"
volumes:
- type: bind
source: ./apps/lu-backend
target: /var/www
- type: bind
source: ./docker/lu-backend/nginx/lumaly.conf
target: /etc/nginx/conf.d/lumaly.conf
- type: bind
source: ./docker/lu-backend/nginx/lumaly.conf
target: /etc/nginx/sites-enable/lumaly.conf
和lumaly.conf
server {
server_name lu-backend.local.com;
listen 80;
root /var/www/public;
index index.php index.html index.htm;
location /storage/ {
alias /var/www/storage/app/public/;
autoindex off;
}
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
try_files $uri /index.php =404;
client_max_body_size 50m;
fastcgi_pass lu-backend-php:9000;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
fastcgi_read_timeout 3600;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /var/www/public/index.php;
fastcgi_param PHP_VALUE "error_log=/var/log/nginx/php-lumaly_errors.log";
}
access_log /var/log/nginx/php-lumaly_service.access.log;
error_log /var/log/nginx/php-lumaly_service.error.log crit;
location ~ /\.ht {
deny all;
}
}
当我尝试打开https://lu-backend.local.com时,我看到了项目页面,但资产(css和js文件)没有加载,因为由于某种原因它们尝试通过http模式加载,无法加载
如果我添加到
app/Providers/AppServiceProvider.php
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
$current = \URL::current();
// if(config('app.env') === 'local') {
// \URL::forceScheme('https');
// }
}
我面临http模式,因为SERVER var不包含HTTPS密钥。
\Symfony\Component\HttpFoundation\Request::isSecure
:
$https = $this->server->get('HTTPS'); // return null
return !empty($https) && 'off' !== strtolower($https);
有返回的 docker compose ps
lu_lu-backend-mariadb_1_807f9a7f6096 docker-entrypoint.sh --def ... Up 0.0.0.0:8306->3306/tcp
lu_lu-backend-nginx_1_b2df3bd1ddc1 /docker-entrypoint.sh ngin ... Up 80/tcp
lu_lu-backend-php_1_566405992c8f bash /usr/local/bin/docker ... Up 9000/tcp
lu_lu-redis_1_12cc7e7d897e docker-entrypoint.sh redis ... Up 0.0.0.0:6379->6379/tcp
lu_lu-traefik_1_78171c03c3d5 /entrypoint.sh traefik Up 0.0.0.0:4200->4200/tcp,
0.0.0.0:443->443/tcp, 0.0.0.0:80->80/tcp,
0.0.0.0:8080->8080/tcp
那么,我的问题是如何使用 nginx 配置 treafic 以便与 https 模式协同工作?
我没有阅读您的整个设置,但您不需要从 nginx 容器公开端口 80。
也许只是尝试删除
expose:
- "80"
我有同样的问题。你解决了吗?