简体   繁体   English

HTTP 重定向到 nginx.conf 中的 HTTPS

[英]HTTP redirected to HTTPS in nginx.conf

I have a nginx.conf in which I am running an application on localhost.我有一个 nginx.conf,我在 localhost 上运行一个应用程序。 I need to redirect the application from HTTP to HTTPS.我需要将应用程序从 HTTP 重定向到 HTTPS。 In the nginx.conf , I have a configuration as below:nginx.conf ,我有如下配置:


http {
  error_log /etc/nginx/error/error.log warn; #./nginx/error.log warn;
  client_max_body_size 20m;

  proxy_cache_path /etc/nginx/cache keys_zone=one:500m max_size=1000m;

  server {
    listen 80;
    server_name localhost; 
    return 301 https://$server_name$request_uri;
     }

  server {
    listen 443 ssl http2;

    server_name localhost; 

    ssl_session_cache shared:SSL:50m;
    ssl_session_timeout 1d;
    ssl_session_tickets off;

    ssl_certificate /etc/nginx/ssl.crt;
    ssl_certificate_key /etc/nginx/ssl.key;

    ssl_protocols TLSv1.2;
    ssl_ciphers EECDH+AESGCM:EDH+AESGCM:EECDH:EDH:!MD5:!RC4:!LOW:!MEDIUM:!CAMELLIA:!ECDSA:!DES:!DSS:!3DES:!NULL;
    ssl_prefer_server_ciphers on;

    keepalive_timeout 70;

    location / {
      proxy_pass http://localhost:80; 
      proxy_ssl_certificate /etc/nginx/ssl.crt; 
      proxy_ssl_certificate_key /etc/nginx/ssl.key; 
      proxy_ssl_verify    off;
      allow all;
      proxy_redirect          off;
      proxy_set_header   Host $host;
      proxy_set_header   X-Real-IP $remote_addr;
      proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header   X-Forwarded-Host $server_name;
      proxy_set_header   X-Forwarded-Proto https;
      #access_log      /var/log/nginx/access.log;
      #error_log       /var/log/nginx/error.log;
      client_max_body_size    0;
      client_body_buffer_size 128k;
      proxy_connect_timeout   1200s;
      proxy_send_timeout      1200s;
      proxy_read_timeout      1200s;
      proxy_buffers           32 4k;
    }

    }

And docker-compose.yml as below:-和 docker-compose.yml 如下:-

version: '2'
services:

  mysql:
    image: mysql:5.7.21
    restart: always
    environment:
    - MYSQL_ROOT_PASSWORD=admin
    - MYSQL_DATABASE=bookstack
    - MYSQL_USER=bookstack
    - MYSQL_PASSWORD=admin
    volumes:
    - ./mysql:/var/lib/mysql
    networks:
    - bookstack-bridge


  bookstack:
    
    image: solidnerd/bookstack:latest
    container_name: bookstack
    restart: always
    depends_on:
    - mysql
    environment:
 
    - APP_URL=http://localhost:8080

    volumes:
    - ./uploads:/var/www/bookstack/public/uploads
    - ./storage-uploads:/var/www/bookstack/public/storage
    ports:
    - 8080:8080
    networks:
    - bookstack-bridge

  nginx:
    image: nginx:latest
    container_name: bookstack-nginx
    restart: always


And in the docker-compose.yml, I do have APP_URL=http://localhost:8080 env variable.在 docker-compose.yml 中,我确实有APP_URL=http://localhost:8080变量。

Does anybody have an idea, what needs to be changed to redirect from HTTP to HTTPS?有没有人知道,从 HTTP 重定向到 HTTPS 需要更改什么?

Thanks in advance.提前致谢。

I customized your docker-compose-yml.我定制了你的 docker-compose-yml。

  1. Your docker-compose.yml would not work for https because some parts are wrong or missing.您的 docker-compose.yml 不适用于 https,因为某些部分错误或丢失。

  2. To use HTTPS you have to create the certificates with Openssl.要使用 HTTPS,您必须使用 Openssl 创建证书。 These must be in the folder /etc/nginx/certs in the container.这些必须在容器中的文件夹 /etc/nginx/certs 中。

  3. When you put the certificates in the folder you have to set - VIRTUAL_PORT=8080 to 443 and change the APP_URL from http to https当您将证书放在文件夹中时,您必须将 - VIRTUAL_PORT=8080 设置为 443 并将 APP_URL 从 http 更改为 https

  4. When you start a service and assign it to the network "web" nginx automatically sees that a new service is registered.当您启动服务并将其分配给网络“web”时,nginx 会自动看到注册了一个新服务。 It automatically maps to the port specified in the image.它会自动映射到映像中指定的端口。 This happens with the volume command "/tmp/docker.sock:ro".这发生在音量命令“/tmp/docker.sock:ro”上。 ":ro" stands for Readonly ":ro" 代表只读

  5. If you assign a service to the network "internal" it is not accessible from the outside and Nginx ignores it.如果您将服务分配给“内部”网络,则无法从外部访问它,Nginx 会忽略它。 See "mysql" service.请参阅“mysql”服务。

  6. With "depends_on:" i say that all services have to start before bookstack starts.对于“depends_on:”,我说所有服务都必须在 bookstack 启动之前启动。 This is important!这个很重要! First Nginx, then MySql and finally bookstack.首先是 Nginx,然后是 MySql,最后是书架。

  7. I prefer to use VIRTUAL_HOST on its own local domain.我更喜欢在自己的本地域上使用 VIRTUAL_HOST。 You can also use localhost there, the only important thing is that your "hosts" file in the operating system points to your external Docker IP.您也可以在那里使用 localhost,唯一重要的是操作系统中的“hosts”文件指向您的外部 Docker IP。 Example: "192.168.5.121 bookstack.local"示例:“192.168.5.121 bookstack.local”

  8. My tip!我的提示! I would store the service "nginx--proxy" in a sepparate docker-compose file.我会将服务“nginx--proxy”存储在单独的 docker-compose 文件中。 Then you can easily register further services with the nginx.然后,您可以轻松地向 nginx 注册更多服务。

Good luck with that and if you want to use Bookstack only locally HTTPS might not be that urgent now.祝你好运,如果你只想在本地使用 Bookstack,HTTPS 现在可能并不那么紧迫。 Otherwise search for "Create Certs for Nginx local"否则搜索“为 Nginx 本地创建证书”

Before you start create the network "web":在开始创建网络“web”之前:

docker network create web
version: '2.4'

services:

  mysql:
    image: mysql:5.7.21
    container_name: bookstack-mysql
    restart: unless-stopped
    networks:
      - "internal"
    healthcheck:
      test: "exit 0"
    environment:
      - MYSQL_ROOT_PASSWORD=admin
      - MYSQL_DATABASE=bookstack
      - MYSQL_USER=bookstack
      - MYSQL_PASSWORD=admin
    volumes:
      - ./docker/data/mysql:/var/lib/mysql

  bookstack:
    image: solidnerd/bookstack:0.29.3
    container_name: bookstack
    restart: unless-stopped
    networks:
      - "web"
      - "internal"
    depends_on:
      nginx--proxy:
        condition: service_started
      mysql:
        condition: service_healthy
    environment:
      - VIRTUAL_HOST=bookstack.local
      - VIRTUAL_PORT=8080
      - DB_HOST=mysql:3306
      - DB_DATABASE=bookstack
      - DB_USERNAME=bookstack
      - DB_PASSWORD=admin
      - APP_URL=http://bookstack.local
    volumes:
      - ./docker/data/uploads:/var/www/bookstack/public/uploads
      - ./docker/data/storage-uploads:/var/www/bookstack/storage/uploads

  nginx--proxy:
    image: jwilder/nginx-proxy:latest
    container_name: nginx--proxy
    restart: always
    environment:
      DEFAULT_HOST: default.vhost
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./docker/data/certs:/etc/nginx/certs
      - /var/run/docker.sock:/tmp/docker.sock:ro
    networks:
      - "web"
      - "internal"

networks:
  web:
    external: true
  internal:
    external: false

The solution worked for me:-该解决方案对我有用:-

In the docker-compose.yml, in nginx service section added networks tag-在 docker-compose.yml 中,在 nginx 服务部分添加了网络标签-

    networks:
    - bookstack-bridge

And in the nginx.conf added proxy_pass as-并在 nginx.conf 添加了 proxy_pass as-

proxy_pass http://bookstack:8080;

Thanks you guys for your help.谢谢你们的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM