title: AWS elastic beanstalk redirect www to non-www with nginx location?
body: <p>I'm trying to redirect www to non-www on elastic beanstalk, but I can't find any info on how to do that with nginx.</p>
  
  <p>I have a location config that redirects http to https. Is there a way I can do this inside that config? I'd have to override the base nginx config to add a <code>server</code> config (which is the recommended way to do it according to the <a href="http://nginx.org/en/docs/http/converting_rewrite_rules.html" rel="nofollow noreferrer">nginx docs</a>), which I'd rather not do if I don't have to.</p>
  
  <pre><code>location / {
       set $redirect 0;
       if ($http_x_forwarded_proto != "https") {
         set $redirect 1;
       }
       if ($http_user_agent ~* "ELB-HealthChecker") {
         set $redirect 0;
       }
       if ($redirect = 1) {
         return 301 https://$host$request_uri;
       }
  
       proxy_pass          http://127.0.0.1:5000;
       proxy_http_version  1.1;
  
       proxy_set_header    Connection          $connection_upgrade;
       proxy_set_header    Upgrade             $http_upgrade;
       proxy_set_header    Host                $host;
       proxy_set_header    X-Real-IP           $remote_addr;
       proxy_set_header    X-Forwarded-For     $proxy_add_x_forwarded_for;
  }
  </code></pre>