admin管理员组

文章数量:1122832

here is my nginx config file,

server {
    listen 80;
    server_name api.xxxx.io;
    client_max_body_size 50M;

    listen 443 ssl;
    ssl_certificate /etc/nginx/ssl/public.crt;
    ssl_certificate_key /etc/nginx/ssl/private.key;

    location / {
        proxy_pass http://backend:4242;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        set_real_ip_from 103.21.244.0/22;
        real_ip_header CF-Connecting-IP;
    }

    location /socket.io/ {
        proxy_pass http://backend:4242;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        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-NginX-Proxy true;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_cache_bypass $http_upgrade;
    }
}

I can reach the api.xxxx.io with and see the logs. But when I try I can not reach my website and there is no logs. I am also redirecting via cloud flare. Can anyone help

here is my nginx config file,

server {
    listen 80;
    server_name api.xxxx.io;
    client_max_body_size 50M;

    listen 443 ssl;
    ssl_certificate /etc/nginx/ssl/public.crt;
    ssl_certificate_key /etc/nginx/ssl/private.key;

    location / {
        proxy_pass http://backend:4242;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        set_real_ip_from 103.21.244.0/22;
        real_ip_header CF-Connecting-IP;
    }

    location /socket.io/ {
        proxy_pass http://backend:4242;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        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-NginX-Proxy true;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_cache_bypass $http_upgrade;
    }
}

I can reach the api.xxxx.io with http://api.xxxx.io and see the logs. But when I try https://api.xxxx.io I can not reach my website and there is no logs. I am also redirecting via cloud flare. Can anyone help

Share Improve this question asked Nov 21, 2024 at 14:01 Salih Burak ÇoşkunSalih Burak Çoşkun 911 gold badge1 silver badge5 bronze badges 2
  • You probably need to create separate servers for 80 and 443 ports. The instruction – crackanddie Commented Nov 21, 2024 at 16:18
  • In opposite to what already said by the others, I can say that listening for both plain HTTP on port 80 and HTTPS on port 443 using a single server block is ok, and splitting your server block in two most likely wouldn't solve the problem. This is looks like a firewall or CloudFlare setup issue. What is your CloudFlare SSL mode (Off/Flexible/Full/Full (strict))? – Ivan Shatsky Commented Nov 21, 2024 at 20:30
Add a comment  | 

1 Answer 1

Reset to default 2

I recommend trying a configuration like this. It’s important to properly separate ports 80 and 443 to ensure everything works correctly.

server {
    if ($host = api.xxxx.io) {
        return 301 https://$host$request_uri;
    } 

        listen 80;
        server_name api.xxxx.io;
        return 301 https://$host$request_uri;
}

server {
  listen 443 ssl;
  listen [::]:443 ssl;

  server_name api.xxxx.io;
  client_max_body_size 50M;

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

  location / {
      proxy_pass http://backend:4242;
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      set_real_ip_from 103.21.244.0/22;
      real_ip_header CF-Connecting-IP;
  }

  location /socket.io/ {
      proxy_pass http://backend:4242;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "upgrade";
      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-NginX-Proxy true;
      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_cache_bypass $http_upgrade;
  }
}

本文标签: dockerNginx listens http but not httpsStack Overflow