Configuring nginx to work alongside Discourse


#1

**EDIT:**I got it working by adding “include /etc/nginx/sites-enabled/*;” to nginx.conf and removing the “proxy_http_version 1.1;” line. Thank you to all who tried to help me.

I intend to host another website on the same machine as the one I host Discourse on. I have therefore tried to follow this guide: Advanced Setup and Administration. I can load dicourse by connecting to the ip address and port, but when I try to connect using the domain name, I only get the “Welcome to nginx” page. This is the file I have placed in /etc/nginx/sites-available

upstream discourse {
#fail_timeout is optional; I throw it in to see errors quickly
server 127.0.0.1:4001 fail_timeout=5;
}

configure the virtual host

server {
# replace with your domain name
server_name [REMOVED (MY DOMAIN NAME)];

location / {
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header Host $http_host;
  proxy_redirect off;
  # pass to the upstream discourse server mentioned above
  proxy_pass http://discourse;
}

}

I have created a link to this file, that is placed in /etc/nginx/sites-enabled. I use CentOS 6.5, if that makes any difference.


(Kane York) #2

Have you tried this howto?


#3

Yes, I have. Using the following file saved as /etc/nginx/sites-available/discourse:

server {
listen 80;

change this

server_name forums.[REMOVED].com;

location / {
proxy_pass http://0.0.0.0:4001;
proxy_set_header Host $http_host;
proxy_http_version 1.1;
# TODO how to pass the x-real-ip headers?
}
}


(Kane York) #4

Did you symlink it in sites-enabled?


#5

Yes, I did. The file is in /etc/nginx/sites-available and the link is in /etc/nginx/sites-enabled


#6

Silly questions, but did you restart the nginx service? …and put the port in the app.yml configuration…and rebuild the app?

service nginx restart
./launcher rebuild app


#7

Yes, I have. I can access discourse by the ip address and port, but not through the domain name/nginx.


(Scott) #8

I just found out I’m experiencing the exact same thing. I can access the new Discourse installation using the IP address and port number. So there’s something I am missing that would map the IP and port to the url

I tried to follow the quoted fix above but when I look at the directory structure on my server this is where my websites are located.

/etc/nginx-sp/vhosts.d

For every website I have there is a .conf file as well as a directory named for each site with a .d (example … mysite.d) when I open the .conf file it has this …

###############################################################################

DO NOT EDIT THIS FILE.

Your changes to this file will be overwritten by ServerPilot.

For information on how to customize nginx settings, see

ServerPilot | Customizing the Nginx Configuration

###############################################################################

server {
listen 80;
listen [::]:80;
server_name
dev.mysite.com
www.dev.mysite.com
;

 root   /srv/users/serverpilot/apps/mysite/public;
 access_log  /srv/users/serverpilot/log/mysite/mysite_nginx.access.log  main;
 error_log  /srv/users/serverpilot/log/mysite/mysite_nginx.error.log;
 proxy_set_header    Host              $host;
 proxy_set_header    X-Real-IP         $remote_addr;
 proxy_set_header    X-Forwarded-For   $proxy_add_x_forwarded_for;
 include /etc/nginx-sp/vhosts.d/mysite.d/*.nonssl_conf;
 include /etc/nginx-sp/vhosts.d/mysite.d/*.conf;

}

and the mysite.d directory has a main.conf file that looks like this …

###############################################################################

DO NOT EDIT THIS FILE.

Your changes to this file will be overwritten by ServerPilot.

For information on how to customize nginx settings, see

ServerPilot | Customizing the Nginx Configuration

###############################################################################

Send all requests to apache.

location / {
proxy_pass http://127.0.0.1:81;
}

I tried to change the listen port to 85 to match what I have in the app.yml file but after restarting nginx `I get this in the terminal window.

root@ns4008540:/var/discourse# service nginx-sp restart
Restarting nginx-sp: nginx: [emerg] bind() to 0.0.0.0:85 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:85 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:85 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:85 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:85 failed (98: Address already in use)
nginx: [emerg] still could not bind()
nginx-sp.
root@ns4008540:/var/discourse#

It seems I am close, but I just need to understand where and what to edit in order for this to work.


Discourse not showing up at specified hostname
(Scott) #9

So … I found this article - Advanced Setup and Administration and used the example proxy_pass. Here is what I changed my .conf file to …

upstream discourse {
#fail_timeout is optional; I throw it in to see errors quickly
server 127.0.0.1:4578 fail_timeout=5;
}

configure the virtual host

server {
# replace with your domain name
server_name dev.mysite.com;

location / {
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header Host $http_host;
  proxy_redirect off;
  # pass to the upstream discourse server mentioned above
  proxy_pass http://discourse;
}

}

When I go to the URL (I use mysite.com as an example) I now get a 502 Bad Gateway error

Is there something that I am missing in the above config, perhaps the http://discourse (I’m not really understanding that one)

Thanks … Pops

EDIT:
WHoooo Hoooo, I found what works with my setup.

In the above .conf files for my configuration, I noticed there was a “proxy_pass” in the main.conf file that sends the request to apache, and I have understood that I don’t need or want Apache in the mix, so … I reverted the first .conf file to it’s original and changed the proxy_pass on the main.conf to be the IP address and port number.

Is there anything wrong with setting it up this way ??

I now have the Discourse forum accessible from the URL

Now, I just need to figure out how I can have more than one Discourse forum running at the same time. Two of my sites will need the forum.


(Kane York) #10

Before you get started trying to do multisite and all that that entails, I have a question. How separate are these two sites? Can you describe them?


(Scott) #11

Sure thing … They’re both Minecraft related, one is specific to the streaming group I am creating. ( chaosxten.com ) and the other is a wide news type site that will cover the Minecraft genre ( not yet created )

The Chaosxten forum I don’t expect to have much traffic, it’s mostly setup so I have a place for open discussion related to any gaming article or stream.

The news site was originally planned to be a digital magazine. Before I go that route, I decided to get a proof of concept first and run it as a news portal. This site has the potential to get big if done properly.

The Discourse install I just did is on a dev branch for all my web properties, so at the very least I would like to keep that and one production version but I could always install a dev version on my local computer, if that turns out to be easier.


(Kane York) #12

I think you can get away with those being on the same forum. Remember to go back and look at your categories after a month, think “is there any of these we don’t really need? is there any category we need to split?”