5

I currently have nginx setup for my server at my.server.com. Using the current configuration I access different applications using http://my.server.com/app1 or http://my.server.com/app2.

I have an apps.conf placed in /etc/nginx/sites-enabled/ this is what it looks like:

upstream app1_servers {
    server 172.12.11.10:8080;
}
upstream app2_servers{
    server 172.12.11.10:9090;
}
server {
    listen 80;
    server_name my.server.com;
    return 301 https://my.server.com/$request_uri; #force https
}

server {
    listen 443;
    ssl on;
    ssl_certificate /etc/ssl/my.server.com/cert.pem;
    ssl_certificate_key /etc/ssl/my.server.com/priv.pem;
    server_name my.server.com;

    location /app1 {
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Host $host;
            proxy_set_header X-Forwarded-Server $host;
            proxy_set_header X-Forwarded-Proto https;
            proxy_pass http://app1_servers/app1;
            proxy_redirect http://$host https://$host;
          proxy_set_header Host $host;
    }
    location /app2 {
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Host $host;
            proxy_set_header X-Forwarded-Server $host;
            proxy_set_header X-Forwarded-Proto https;
            proxy_pass http://app2_servers/app2;
            proxy_redirect http://$host https://$host;
            proxy_set_header Host $host;
    }
}

Question

The above works fine. However, now I would like to change how I access app1 and app2. I would like to access them by http://app1.my.server.com and http://app2.my.server.com while still maintaining force ssl and doing proxy_pass

What configuration changes do I need to make for this to take effect? Additionally, I would like to keep configuration settings for each application in its separate file.

1 Answer 1

0

This should be pretty easy. You just need create server blocks for each subdomain, get an SSL certificate for the subdomain (unless the main cert is a wildcard cert), and copy the applicable location blocks in with a couple of tweaks.

Something like this should work, though I haven't tested it so it will almost certainly need some tweaking.

server {
  listen 80;
  server_name app1.server.com;
  return 301 https://app1.server.com/$request_uri; #force https
}

server {
  listen 443;
  ssl on;
  ssl_certificate /etc/ssl/app1.server.com/cert.pem; # changed
  ssl_certificate_key /etc/ssl/app1.server.com/priv.pem; # changed
  server_name app1.server.com; # changed

  location / { # changed
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-Proto https;
        proxy_pass http://app1_servers/app1;
        proxy_redirect http://$host https://$host;
        proxy_set_header Host $host;
  }

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .