英文:
Docker Laravel: Redirecting to https
问题
我正在尝试将我的现有Laravel应用程序“dockerize”。以下是我正在进行的操作。
目录结构如下:
phirater-l51
.. <Laravel应用程序>
Dockerfile
docker
mysql
init
01-databases.sql
my.cnf
nginx
default.conf
docker-compose.yml
这是我的docker-compose.yml文件:
version: "3.7"
services:
app:
container_name: app
build:
args:
user: ${USER}
uid: ${UID}
context: ./../phirater-l51
dockerfile: ./../phirater-l51/Dockerfile
working_dir: /var/www/
environment:
- COMPOSER_MEMORY_LIMIT=-1
depends_on:
- db
image: phirater-l51:latest
volumes:
- ./../phirater-l51:/var/www
networks:
- phios
db:
image: mysql:8.0
container_name: db
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- dbdata:/var/lib/mysql
- ./mysql/my.cnf:/etc/mysql/my.cnf
- ./mysql/init:/docker-entrypoint-initdb.d
ports:
- "3307:3306"
networks:
- phios
nginx:
container_name: nginx
image: nginx:alpine
ports:
- "8005:80"
depends_on:
- app
- db
volumes:
- ./../phirater-l51:/var/www
- ./nginx/:/etc/nginx/conf.d
networks:
- phios
cache:
container_name: cache
image: redis:alpine
volumes:
- cachedata:/data
networks:
- phios
networks:
phios:
driver: bridge
volumes:
dbdata:
driver: local
cachedata:
driver: local
这是我的nginx/default.conf文件:
server {
listen 80;
index index.php index.html;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/public;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass app:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
gzip_static on;
}
}
现在,当我运行容器时,它正常工作,但是当我尝试从终端运行curl localhost:8005
时,它返回以下内容:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta http-equiv="refresh" content="0;url='https://localhost:8005'" />
<title>Redirecting to https://localhost:8005</title>
</head>
<body>
Redirecting to <a href="https://localhost:8005">https://localhost:8005</a>.
</body>
</html>
当我运行curl -I
时,我得到以下头信息:
HTTP/1.1 302 Found
Server: nginx/1.25.1
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/7.4.33
Cache-Control: no-cache, private
Date: Thu, 20 Jul 2023 06:44:34 GMT
Location: https://localhost:8005
Set-Cookie: laravel_session=<session>; path=/; secure; httponly
当我尝试在浏览器中执行时,我收到以下错误:
此站点无法提供安全连接,本地主机发送了无效的响应。ERR_SSL_PROTOCOL_ERROR
在这里我做错了什么?
英文:
So I am trying to dockerize
my existing laravel
application. Here is what I am doing.
The dir
structure is like this:
phirater-l51
.. <laravel applciation>
Dockerfile
docker
mysql
init
01-databases.sql
my.cnf
nginx
default.conf
docker-compose.yml
Here is my docker-compose.yml file:
version: "3.7"
services:
app:
container_name: app
build:
args:
user: ${USER}
uid: ${UID}
context: ./../phirater-l51
dockerfile: ./../phirater-l51/Dockerfile
working_dir: /var/www/
environment:
- COMPOSER_MEMORY_LIMIT=-1
depends_on:
- db
image: phirater-l51:latest
volumes:
- ./../phirater-l51:/var/www
networks:
- phios
db:
image: mysql:8.0
container_name: db
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- dbdata:/var/lib/mysql
- ./mysql/my.cnf:/etc/mysql/my.cnf
- ./mysql/init:/docker-entrypoint-initdb.d
ports:
- "3307:3306"
networks:
- phios
nginx:
container_name: nginx
image: nginx:alpine
ports:
- "8005:80"
depends_on:
- app
- db
volumes:
- ./../phirater-l51:/var/www
- ./nginx/:/etc/nginx/conf.d
networks:
- phios
cache:
container_name: cache
image: redis:alpine
volumes:
- cachedata:/data
networks:
- phios
networks:
phios:
driver: bridge
volumes:
dbdata:
driver: local
cachedata:
driver: local
Here is my nginx/default.conf file:
server {
listen 80;
index index.php index.html;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/public;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass app:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
gzip_static on;
}
}
Now when I run the containers it works fine but when I try to do curl localhost:8005 from terminal it returns this:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta http-equiv="refresh" content="0;url='https://localhost:8005'" />
<title>Redirecting to https://localhost:8005</title>
</head>
<body>
Redirecting to <a href="https://localhost:8005">https://localhost:8005</a>.
</body>
</html>
When I do curl -I, I get these headers:
HTTP/1.1 302 Found
Server: nginx/1.25.1
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/7.4.33
Cache-Control: no-cache, private
Date: Thu, 20 Jul 2023 06:44:34 GMT
Location: https://localhost:8005
Set-Cookie: laravel_session=<session>; path=/; secure; httponly
And when I try to do it in browser I get this:
> This site can’t provide a secure connectionlocalhost sent an invalid
> response. ERR_SSL_PROTOCOL_ERROR
What am I doing wrong here?
答案1
得分: 1
我正在使用自定义的Docker中的Laravel。这是我的NGINX配置文件的一个有效示例,适用于http
,没有重定向问题。http://project.test/
它与官方Laravel建议非常接近:https://laravel.com/docs/10.x/deployment#nginx
server {
listen 80;
server_name project.test;
root /var/www/laravel/public;
index index.php;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
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 app:9000;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
你有几个不符合规范的地方,比如没有server_name
。尝试添加它。
或者选择官方配置文件,检查问题是否解决。之后,你可以根据需要或问题来调整工作配置。
英文:
I'm using Laravel in a custom docker as well. Here an working example of my NGINX config for http
. Works fine without redirect. http://project.test/
It's pretty close to the official Laravel suggestion: https://laravel.com/docs/10.x/deployment#nginx
server {
listen 80;
server_name project.test;
root /var/www/laravel/public;
index index.php;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
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 app:9000;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
You have several deviations like no server_name
.
Try to add it.
Or pick the official config and check if the problem is gone. After that you can adjust the working config until you reach your goal or the problem.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论