Skip to main content Setting up reverse proxy to subdomain : r/nginx
Go to nginx
•
[deleted]

Setting up reverse proxy to subdomain

I have MediaCMS set up on a subdomain, and I would like to be able to access it from path on the root domain. That is, it is currently hosted and accessible at https://media.example.com, and I would like to be able to go to https://example.com/media to use it.

I've tried the following configuration file for the example.com server block, but when I access example.com/media, it gives an error, which I assume is generated by MediaCMS, that simply says "you are lost!". Does anyone know how to get this working?

server {
  root /var/www/example.com/html;
  index index.php index.html index.htm index.nginx-debian.html;
  server_name example.com www.example.com;

  resolver 8.8.8.8;

  location /media/ {
    proxy_set_header Host media.example.com;
    proxy_pass https://media.example.com;
    proxy_set_header X-Forwarded-Proto $scheme;
  }


  location / {
    try_files $uri $uri/ =404;

    location ^~ /phpmyadmin/ {
      auth_basic "Admin Login";
      auth_basic_user_file /etc/nginx/pma_pass;

      location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
      }  
    }
  }


  location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
  }
        

  listen [::]:443 ssl ipv6only=on; # managed by Certbot
  listen 443 ssl; # managed by Certbot
  ssl_certificate /etc/letsencrypt/media/example.com/fullchain.pem; # managed>
  ssl_certificate_key /etc/letsencrypt/media/example.com/privkey.pem; # manag>
  include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
  ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot


}

server {
  if ($host = www.example.com) {
    return 301 https://$host$request_uri;
  } # managed by Certbot


  if ($host = example.com) {
    return 301 https://$host$request_uri;
  } # managed by Certbot


  listen 80;
  listen [::]:80;

  server_name example.com www.example.com;
  return 404; # managed by Certbot
}
Korbit’s AI-Powered Code Review Agent helps your team ship better code, faster.
Thumbnail image: Korbit’s AI-Powered Code Review Agent helps your team ship better code, faster.
Sort by:
Best
Open comment sort options

You want to access it both ways in parallel? (Some Web applications need to know where they live, so I guess you would have to work with URL rewrites to fool the CMS). (I haven‘t studied your input in detail so far)

[deleted]
•

As long as it works at example.com/media, I don't need it to work at media.example.com

More replies
More replies
[deleted]
•

Comment deleted by user

[deleted]
•

Ok, I see. What I'm hoping to do is put the CMS app in an iframe on a page in the root domain. And I'd like to be able to inject javascript into the CMS iframe, but I saw that browsers restrict javascript injection to the same hostname, so I wanted to put it on the same one.

I just found that it is possible to inject into a different subdomain of the same root, if you can add a line into the iframe's javascript. And I think MediaCMS allows editing the javascript, so I'll give that a try.

[deleted]
•

Did you ever figure this out?

[deleted]
•

Oh I don't remember, sorry. I abandoned that project a while ago.

More replies