1

I've asked this question before and I think I found a potential answer on the nginx Website, which is to have multiple internal IPs for the Web server to listen on. I don't know how to do that with an ec2 on AWS so here is the question again:

The real problem is that I can't do this for multiple sites, like example2.com, example3.com... if I do that, I end up with endless redirects. The first site I set up always works, but the others do not.

My server blocks for first domain example.com:

# Everything to http goes to https
server {
    listen 80;
    server_name www.example.com example.com;
    return 301 https://example.com$request_uri;
}

# All requests for www.example.com go to example.com
# The http case is handled above, this is just for https://www...
server {
    listen 443 ssl;
    server_name www.example.com;
    ssl_certificate /etc/ssl/certs/example-cloudflare.pem;
    ssl_certificate_key /etc/ssl/private/example-cloudflare.key;
    return 301 https://example.com$request_uri;
}

# The real server block that handles the request
server {

    listen 443 ssl;

    server_name example.com;

    # SSL stuff
    ssl_certificate /etc/ssl/certs/example-cloudflare.pem;
    ssl_certificate_key /etc/ssl/private/example-cloudflare.key;

    # Document root
    root /var/www/example.com/html;

    # By default this handles the directory index
    index index.php;

    # Logs. main is defined elsewhere.
    error_log /var/log/nginx/example-error.log warn;
    access_log /var/log/nginx/testsite-access.log main;

    # First try file, then directory, else 404 error
    location / {
         try_files $uri $uri/ =404;
    }

    # Handle PHP files
    location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }

}

This works for the first site as I've mentioned, but when I copy it and enable it with say example2.com and a new document root and ssl certificate and key file etc, it seems to work fine but I end up with and endless redirect loop when it gets to https://example2.com/.

Automatic SSL rewrites and redirects are disabled with Cloudflare, so it's not that. It's an issue with my nginx configuration I think.

I'm not sure how to diagnose this problem and why there would be an endless redirect loop for additional sites, but not the first one I configure.

I also have difficulty getting SSL virtual hosts happening for Apache, but that's not relevant here; just generally having problems with multiple SSL sites on the same web server, in this case an ec2 on AWS with Ubuntu 16.04 Xenial with nginx.

  • What happens if you temporarily remove the server blocks that issue redirects? – Stephen Ostermiller Apr 24 '18 at 9:58

Your Answer

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

Browse other questions tagged or ask your own question.