Passing subdomain request from cloudflare to nginx

I have one server with two services working in different ports.
Previously I used A record with page rules to redirect my subdomain blog.domain.com to 127.0.0.1:2368,
but I realised the subdomain name doesn’t mask the ip address on browser’s address bar.

So I added nginx and proxied both my web and the subdomain to each port.
it works fine in my local, but when I deployed my changes, cloudflare doesn’t forward my subdomain request to the server.

How to make my server get internet request to the subdomain?
Will it be masking the ip address if I use in this way? If not, any suggestion?

The standard way is to simply set-up A/AAAA records in the DNS page pointing to your server, set them to proxied (:orange:) and then configure nginx to handle two websites (as they are technically).

I tried using A records from my subdomain to my server, but seems something wrong because it shows my web instead of my blog. Can’t use AAAA records, because it is for IPv6.

So I guess something still wrong with my nginx config?

You solved your issue, you have configured nginx so that it replies to any request with your website, even if I set-up my domain to point to your service. It’s not a great place to leave a server at…

hmm, I’m sorry not really get what you mean…

currently blog.wedbio.com still not showing my blog, it is showing wedbio.com.
I added another service with port 2368 to serve my blog, but seems not working in my server, but it works locally.

It won’t work with another port, you need to have both on the same ports (80 & 443) in the same nginx instance…

but it works fine in my local?
I am using docker, nginx in local get hit when I open localhost or blog.localhost, but in production request to wedbio.com or blog.wedbio.com doesn’t hit my nginx.

version: "3"
services:
  proxy:
    image: nginx:alpine
    build:
      context: ./nginx
    ports:
      - "80:80"
    networks:
      - ewed
  eweddings:
    image: ewedding
    build:
      context: .
      dockerfile: docker/Dockerfile
    ports:
      - "443:443"
      - "8080:8080"
    networks:
      - ewed

  blog:
    image: ghost
    volumes:
      - "./content:/var/www/ghost/content"
    ports:
      - "2368:2368"
    environment:
      - url:https://blog.wedbio.com
    networks:
      - ewed

my nginx default.conf:

server {
    listen 443 ssl http2 default_server;
    listen [::]:443 ssl http2 default_server;
    server_name localhost wedbio.com www.wedbio.com;

    ssl_certificate snippets/wedbio.crt;
    ssl_certificate_key snippets/wedbio.key;

    #charset koi8-r;
    access_log /var/log/nginx/https-wedbio-access.log;
    error_log /var/log/nginx/https-wedbio-error.log debug;

    location / {
	proxy_pass https://ewedding;
        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-Proto https;
        proxy_cache_bypass $http_upgrade;
    }

    error_log /var/log/nginx/host.error.log warn;
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

server {
		listen       80;
		listen [::]:80;

		server_name localhost wedbio.com www.wedbio.com;

		access_log /var/log/nginx/http-wedbio-access.log;
    error_log /var/log/nginx/http-wedbio-error.log debug;

    return 302 https://$server_name$request_uri;
}

server {
		listen       80;
		listen [::]:80;

		server_name blog.localhost blog.wedbio.com;

		access_log /var/log/nginx/http-blog-access.log;
		error_log /var/log/nginx/http-blog-error.log debug;

		location / {
		    proxy_set_header Host $host;
		    proxy_pass http://blog:2368/;
				proxy_redirect off;
		}

		error_page   500 502 503 504  /50x.html;
		location = /50x.html {
		    root   /usr/share/nginx/html;
		}
}

the access_log and error_log files are empty in prod, but showed some logs in my local

Taking a quick look (and this is not the place for nginx questions, you will find better answers elsewhere) you seem to be missing the https block for the blog subdomain, localhost works because it’s http.

I’m not sure it is related to the nginx config itself… yes u’re right I didn’t put https block for blog because I didn’t configure https in my blog.

But I put access_log and error_log to each block, it should shows the log when someone access wedbio.com or blog.wedbio.com via browser. it shows something like this:

172.19.0.1 - - [20/Jan/2020:12:01:07 +0000] "GET / HTTP/1.1" 200 4907 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_2) AppleWebKit/537.36 (KHTML, like Gecko)"
172.19.0.1 - - [20/Jan/2020:12:01:07 +0000] "GET /assets/built/screen.css?v=b1adfb6276 HTTP/1.1" 200 8935 "http://blog.localhost/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_2) AppleWebKit/537.36 (KHTML, like Gecko)"
172.19.0.1 - - [20/Jan/2020:12:01:07 +0000] "GET /assets/built/casper.js?v=b1adfb6276 HTTP/1.1" 200 1477 "http://blog.localhost/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_2) AppleWebKit/537.36 (KHTML, like Gecko)"
172.19.0.1 - - [20/Jan/2020:12:01:11 +0000] "GET /favicon.ico HTTP/1.1" 200 1143 "http://blog.localhost/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_2) AppleWebKit/537.36 (KHTML, like Gecko)"

but in my prod server, it is empty which means request from browser go through cloudflare but never call nginx…? CMIIW

If you see the files showing up on the browser and they are correct they go the server. I really believe the issue is with the configuration. Cloudflare wise there is basically nothing to configure.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.