Docker部署Ruby/Rails应用自动跳转HTTPS致本地访问异常求助
问题
我将一个Ruby应用Docker化后,本地访问时所有浏览器都会从http://0.0.0.0:3000重定向到https://0.0.0.0:3000,导致浏览器报错:
This site can’t provide a secure connection 0.0.0.0 sent an invalid response.
我已尝试清除缓存、浏览记录、重启设备,并在Docker Compose的环境变量中添加了HTTPS_METHOD: noredirect,但问题仍未解决。直接通过Rails server访问http://127.0.0.1:3000时不会出现重定向,可正常访问,因此推测问题出在Dockerfile或Docker Compose配置中。现附上相关配置文件,恳请提供解决建议。
Dockerfile
FROM ruby:2.7.7-alpine3.16 AS builder ENV RAILS_ENV production ENV RAILS_SERVE_STATIC_FILES true ENV RAILS_LOG_TO_STDOUT true ENV SECRET_KEY_BASE 1 # RUN mkdir /app # WORKDIR /app # Install required libraries on Alpine # note: build-base required for nokogiri gem # note: postgresql-dv required for pg gem RUN apk update && \ apk add tzdata postgresql-dev && \ apk add postgresql-client && \ apk add nodejs yarn && \ apk add build-base && \ gem install rails bundler && \ # Throw errors if Gemfile has been modified since Gemfile.lock bundle config --global frozen 1 # Copy Gemfile so we can cache gems COPY Gemfile Gemfile.lock ./ # Install Ruby gems RUN bundle install --without development test # Copy all application files COPY . . # Precompile assets RUN bundle exec rails assets:precompile && \ bundle exec rails webpacker:compile RUN chmod +x entrypoint.sh CMD ["/entrypoint.sh"] EXPOSE 3000
Docker Compose配置
version: "3.8" services: rails: build: dockerfile: Dockerfile context: . env_file: - rails.env environment: HTTPS_METHOD: noredirect DB_USERNAME: xxxxxxxx DB_PASSWORD: .env DB_DATABASE: xxxxxx_production DB_PORT: 5432 DB_HOST: db RAILS_MAX_THREADS: 5 ports: - "3000:3000" restart: always depends_on: - "db" db: image: postgres:13.9-alpine env_file: - db.env environment: HTTPS_METHOD: noredirect POSTGRES_USER: xxxxxxx PGDATA: /var/lib/postgresql/data/pgdata restart: always volumes: - pgdata:/var/lib/postgresql/data volumes: pgdata:
entrypoint.sh脚本
#!/bin/sh # Note: !/bin/sh must be at the top of the line, # Alpine doesn't have bash so we need to use shell. # Docker entrypoint script. # Don't forget to give this file execution rights via `chmod +x entrypoint.sh` # which I've added to the Dockerfile but you could do this manually instead. # Wait until Postgres is ready before running the next step. while ! pg_isready -q -h $DB_HOST -p $DB_PORT -U $DB_USERNAME do echo "$(date) - waiting for database to start." sleep 2 done # If the database exists, migrate. Otherwise setup (create and migrate) echo "Running database migrations..." bundle exec rails db:migrate 2>/dev/null || bundle exec rails db:create db:migrate echo "Finished running database migrations." # Remove a potentially pre-existing server.pid for Rails. echo "Deleting server.pid file..." rm -f /tmp/pids/server.pid # Start the server. echo "Starting rails server..." bundle exec rails server
解决建议
- 禁用Rails生产环境强制HTTPS:在Docker Compose的rails服务
environment块中添加RAILS_FORCE_SSL: false。你之前设置的HTTPS_METHOD是Nginx的配置项,对Rails自身的重定向逻辑无效,而Rails生产环境默认开启force_ssl,这是导致HTTP重定向到HTTPS的核心原因。 - 明确指定Rails服务器绑定地址:修改entrypoint.sh中的启动命令为
bundle exec rails server -b 0.0.0.0,确保服务器监听容器内所有网络接口,避免因绑定地址问题引发的异常。 - 检查
rails.env文件:确认该文件中没有设置FORCE_SSL=true或其他会触发HTTPS重定向的配置,防止环境变量冲突。 - 本地访问改用
127.0.0.1:建议访问http://127.0.0.1:3000而非http://0.0.0.0:3000,部分浏览器对0.0.0.0的处理可能引发异常。 - 修改Rails配置文件(兜底方案):如果环境变量不生效,直接编辑
config/environments/production.rb,将config.force_ssl = true改为config.force_ssl = false,或者改为动态读取环境变量:config.force_ssl = ENV.fetch("RAILS_FORCE_SSL", "false") == "true",这样可以灵活控制是否开启强制HTTPS。
内容的提问来源于stack exchange,提问作者GidB
相关产品推荐
相关产品推荐

