How do I do what I did in htaccess in nginx?


#1

Hey there,
I just switched over to Discourse, I was testing Flarum before with Apache and htaccess and I had a rule written up that whenever there was a subdomain (excluding www.) it would take that subdomain and append it to the end of the url to simulate a dynamic subdomains and I didn’t have to actually make them for them to work how I wanted. Example being if you type “abc.domain.com” it would end up as “domain.com/forum/t/abc” which would drop you in the abc section of the forum. Is it possible to do this with Discourse using nginx?

Looking at how Discourse is laid out, I would want it to be abc.domain.com takes you to domain.com/forum/c/abc/ If anyone has any insight on this, I would appreciate it!

Thanks,


(OG) #2

Use nginx outside of docker container (see Running other websites on the same machine as Discourse) and you can rewrite those URLs using nginx configuration.

Syntax is:

server {
  server_name  sub.domain.com;
  rewrite ^(.*) http://12.34.56.78/a/particular/path/ permanent;
}

#3

I am already running it externally, I just wasnt sure how to go about that part. Thanks! I will give that a go now.


There is more to it isn’t there? Doesn’t a variable have to go at the end of the path based on the subdomain? It will not always be the same. Here is how I had it before.

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

Ah, I found this! htaccess to nginx converter
A htaccess to nginx converter, that is convenient, lol.

If I am already using proxypass and such, do I just put this in location along with all that? or does it have to go before or after it or something?

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

Right now it 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;

}

I just tested it out some more, it turns in to a loopy mess, lol.


(OG) #4

See the link I gave you … Running other websites on the same machine as Discourse

I’m sure you can derive solution from it…


#5

I mean, you might think that but when I went to test it, it just gave me a loop. In no way does the link you posted explain anywhere near how you would go about merging this:

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

With this properly

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;
 }

(OG) #6

Read the manual, man…

Location blocks live within server contexts


#7

I am not a server genius / programmer. I just was asking if someone could help. I appreciate you the information, but if I was able to figure it out and get it working, I would not be here asking.


(Kane York) #8

@Overgrow, what @MostHated needs here is NGINX rewrite rule help, to a level that I think is beyond that of Meta to provide.

@MostHated, as this is a general NGINX question and not specific to Discourse, I suggest asking your question on a more specialized forum.


#9

That is fine, I just figured since other people had used the proxy stuff on here before and there were quite a few developers that someone might have some insight.

Thanks,


(OG) #10

Understanding the Nginx Configuration File Structure and Configuration Contexts | DigitalOcean
Server Block Examples | NGINX

I have pointed to the tools you need. And there are hundreds of nginx tutorials online. Nothing personal, but if that is not enough, you need to hire admin to do it for you.


(Jeff Atwood) #12