4

I have this nginx config for my website on https where nginx is used as a reverse proxy server:

  server {
      listen 80 default_server;
      listen [::]:80 default_server;
      server_name my_domain123.com www.my_domain123.com;
      return 301 https://$server_name$request_uri;
  }

  server {
    listen 443 ssl;
    listen [::]:443 ssl;
    server_name localhost www.my_domain123.com;
    return 301 https://my_domain123.com$request_uri;
  }

  server {
      listen 443 ssl default_server;
      listen [::]:443 ssl default_server;
      server_name my_domain123.com;

    location / {
      proxy_redirect      http://localhost:4000 https://my_domain123.com;
      # ...........................

    }

How should I adjust it so that I can host multiple websites with different domain names on the same server? Where in the config exactly should I insert the new configuration for that new website?

Or should I create one more site-available/enabled for it also? Yet, the question remains: how would I combine 2 or more configurations -- same server, multiple domains -- properly?

  • What's wrong with the way you show in your question? – Sven Dec 5 '17 at 9:09
  • @Sven, show what? – Rakori Dec 5 '17 at 9:31
  • The config excerpt in your question with multiple server blocks is exactly how you configure nginx to serve multiple domains - what more do you need? – Sven Dec 5 '17 at 9:37
  • @Sven, that's for a single domain. how exactly add more domains and sections/config for them? – Rakori Dec 5 '17 at 23:31
  • You just have multiple server blocks and add all relevant configuration tsatements to them, as e.g. listen, server_name or proxy_redirect. You can put them all into the same file if you like. – Sven Dec 6 '17 at 9:40
8

Normally you create a new config file /etc/nginx/sites-available/newserver.conf for the new server and link it from /etc/nginx/sites-enabled. To use nginx as reverse proxy, you configure SSL in nginx (ssl_certificate, ...) and in the location section you use proxy_pass to the non SSL server at localhost. proxy_redirect is also needed, but that only modifies the Location header in case your non SSL local server sends one. You find an example in the following article.

Multiple http servers on localhost using different ports

    server {
    server_name mydomain-01.com;

    location / {
      proxy_redirect http://localhost:8001 https://mydomain-01.com;
      ...
    }
}
server {
    server_name mydomain-02.com;

    location / {
      proxy_redirect http://localhost:8002 https://mydomain-02.com;
      ...
    }
}

Single http server on localhost using hostname based sites

server {
    server_name mydomain-01.com;

    location / {
      proxy_redirect http://s1.localdomain:4000 https://mydomain-01.com;
      ...
    }
}
server {
    server_name mydomain-02.com;

    location / {
      proxy_redirect http://s2.localdomain:4000 https://mydomain-02.com;
      ...
    }
}
  • you didn't understand my question – Rakori Dec 5 '17 at 8:26
  • 1
    Do you have a single webserver on localhost:4000 serving all domains or multiple servers on different ports? – 1Peter Dec 5 '17 at 20:29
  • none -- one physical server, but multiple websites on different ports. one website for one domain – Rakori Dec 5 '17 at 23:28
  • I can't figure out -- how can I apply your solution to my case? where are the ports? I have 3 sections per domain -- where are the sections in your answer? – Rakori Dec 5 '17 at 23:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.