0

I have two node.js instances running behind nginx proxy I also have a rewrite rule to push everything to https.

My issue is that I would like to split dev.domain.tld to go to the second node.js instance and everything else to go to the first node.js instance.

This is my conf file

server {
    listen 80;
    server_name  domain.tld;
    rewrite ^(.*) https://www.domain.tld$1 permanent;
}
server {
  listen 443 ssl;
  client_max_body_size 100M;
  client_body_buffer_size 128k;
  server_name www.domain.tld;
  root                    /var/empty;
  ssl_certificate         /etc/ssl/certs/2015/bundle.crt;
  ssl_certificate_key     /etc/ssl/certs/2015/key.pem;

  error_log /tmp/nginx-error.log info;

  location ~ ^/(images|css|font|js) {
    root c;
    expires 365d;
    add_header  Cache-Control public;
    gzip on;
    gzip_vary on;
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript;
  }

  location / {
    proxy_pass http://localhost:3000;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-Proto https;
    proxy_set_header X-Forwarded-Ssl on;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
  }
}

server {
  listen 443 ssl;
  client_max_body_size 100M;
  client_body_buffer_size 128k;
  server_name dev.domain.tld;
  root                    /var/empty;
  ssl_certificate         /etc/ssl/certs/2015/bundle.crt;
  ssl_certificate_key     /etc/ssl/certs/2015/key.pem;

  error_log /tmp/nginx-error.log info;

  location ~ ^/(images|css|font|js) {
    root /var/www/domain.tld/public;
    expires 365d;
    add_header  Cache-Control public;
    gzip on;
    gzip_vary on;
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript;
  }

  location / {
    proxy_pass http://localhost:8080;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-Proto https;
    proxy_set_header X-Forwarded-Ssl on;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
  }
}

I looked at using regex as per How to get dev.*.domain.com to work on nginx question/answer but I am unsure as to where add the rule?

Also, I want to re-use the location ~ ^/(images|css|font|js) { ... block for both - is this possible?

Any advice much appreciated

2
  • What's the actual problem that you're having?
    – womble
    Aug 3, 2015 at 9:37
  • is not re-writing correctly is either using the first rule or the second
    – khinester
    Aug 3, 2015 at 9:45

1 Answer 1

0

For redirecting HTTP to HTTPS I use the form:

server {
    listen       80;
    server_name  sub.domain.tld;
    return       301 https://$server_name$request_uri;
}

To make an instance default then you use default_server keyword. i.e.:

server {
    listen       443 default_server ssl;
    server_name  domain.tld;
}

Hopefully that helps you do want you need.

You must log in to answer this question.

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