This is one of those reminder posts. I just set up nginx with a Go web application, and I always forget how to do it. Three things are required:
- Update nginx.conf to include a sub-directory where the actual conf will be stored
- Add an nginx.conf to the sub-directory
- Update /etc/hosts with your site name
Update nginx.conf
nginx.conf (in /usr/local/etc/nginx if you installed from homebrew). The include path should be relative to the nginx.conf file. If the nginx.conf file is in the nginx dir, then sites-enabled should be in the nginx dir as well.
http {
.....
include /usr/local/etc/nginx/sites-enabled/*;
....
}
Add a new server.nginx.conf file
Add a new conf file in sites-enabled called mysite.nginx.conf
upstream mysite {
server 127.0.0.1:3030;
}
server {
listen 80;
server_name mysite.dev;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_pass http://mysite;
proxy_redirect off;
}
}
A few things:
- The upstream block is like an alias for your application. So nginx is expecting your server to run on 127.0.0.1:3030
- The upstream alias is referenced in the
proxy_passattributehttp://name of upstream alias
Update /etc/hosts
Lastly, update add the server (server_name from server.nginx.conf) to /etc/hosts
... 127.0.0.1 mysite.dev ...
Now start nginx and start your server
# Use sudo when starting nginx in order to run on port 80 $ sudo nginx # Start your go application
Now you should be able to access your application at http://mysite.dev
Update: If you want to allow websocket connections, update the nginx conf like this