Link to home
Start Free TrialLog in
Avatar of MostHated
MostHatedFlag for United States of America

asked on

How can I merge these two nginx rules to work properly?

I was running Apache before using an htaccess rule but switched to nginx running a docker instance of the Discourse forum software. I am running nginx outside of the docker instance and then using proxypass so that it is located in domain.com/forum. Before I had it setup as seen below in order to take whatever subdomain is input minus www. and append it to the end of the url.  If no subdomain is provided, it would just end up working as normal.

    RewriteEngine on
    RewriteCond %{HTTP_HOST} !^www. [NC]
    RewriteCond %{HTTP_HOST} .example.com$ [NC]
    RewriteRule ^(.*)$ http://example.com/forum/t/%1$1 [L,NC,QSA]

Open in new window


I used a converter from htaccess to nginx which gave me this:

    location / {
      if ($http_host !~ "^www\."){
       rewrite ^(.*)$ http://example.com/forum/t/%1$1 redirect;
      }
    }

Open in new window


My current location setup looks like this:

    location /forum {
        proxy_pass http://unix:/var/discourse/shared/standalone/nginx.http.sock:;
        proxy_set_header Host $http_host;
        proxy_http_version 1.1;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto https;
	   }
	    fastcgi_read_timeout 60;
     }

Open in new window


So my question is, how can I merge my converted code in the if statement with my current code that contains the proxy_pass stuff? I tried to just put it in there but that just ended up with a loop.

Any insight would be appreciated,
Thanks!
ASKER CERTIFIED SOLUTION
Avatar of noci
noci

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial