ubuntunginxnginx-reverse-proxy

nginx reverse proxy won't start when using 301 redirects


(very new to this all) I am trying to redirect www.example.com to example.com with this config:

server {
     listen 80;
     server_name www.example.com;
     return 301 https://$example.com$request_uri;
 }
 server {
     listen 80;
     listen [::]:80;
     server_name example.com;
 
     root /var/www/example.com/html;
     index index.html;
 }

This however, causes the nginx proxy to not work and return [emerg] unknown "example" variable

When commenting the return 301 https://$example.com$request_uri; line, the proxy server starts just fine.

I don't know what to try since I can't find anyone who has had a similar problem.


Solution

  • The "$" in front is to tell the server that it's a variable, try to use this instead:

    return 301 https://$server_name$request_uri;

    Or, of course, you can use:

    return 301 https://example.com$request_uri;