Skip to content

Instantly share code, notes, and snippets.

@StevenJL
Last active January 18, 2018 02:32
Show Gist options
  • Save StevenJL/9b547d8ff8de2a52050bddad4a8e6820 to your computer and use it in GitHub Desktop.
Save StevenJL/9b547d8ff8de2a52050bddad4a8e6820 to your computer and use it in GitHub Desktop.
Reverse proxy server
http {
server {
listen 80;
server_name myreverseproxy.com;
# The `upstream` directive defines an upstream server that can be referenced
# in the rest of this file. In this case, it's a web application (ie. Rails/Rack)
# that is listening at the given unix socket.
upstream upstream_server_name {
server unix:/tmp/rack.sock;
}
# The `location` directive takes a pattern it against the
# path of the request URI. In this case, it matches all requests
# that have the prefix "app" in their URI.
location /app {
# The `proxy_pass` directive sets up an upstream server.
# Set the url of the upstream server, so that any request that
# starts with "app" in their URI is forwarded upstream to
# the upstream server "upstream_server_name";
proxy_pass http://upstream_server_name;
}
# Sometimes the upstream server can also be defined using the local
# address and port. For example, this could be a unicorn server process
# (since it's default port is 8080)
upstream upstream_server_two {
# The `keep-alive` directive will kept alive least 5 inactive TCP connections
# with the upstream server, as it can improve performance since the HTTP
# request does not have to do the TCP handshake every time. The downside is
# it is using up connections in the backend server.
keepalive 5;
server 127.0.0.1:8080;
}
# Now every request with URI starting as "another_app" is forwarded
# to the upstream server "upstream_server_two".
location /another_app {
proxy_pass http://upstream_server_two;
}
# An 'upstream` can contain multiple servers which is called a "cluster"
upstream upstream_cluster {
# By default, nginx uses the Round-Robin distribution algorithm
# among the cluster, but you can use weights to weigh the servers more.
server 192.168.1.1 weight=2;
server some.server.com;
server 127.0.0.1:8080;
}
location /to_cluster {
proxy_pass http://upstream_cluster;
}
location /downloads {
# We can also specify an upstream server by an ip address.
# Note the ip is a private address meaning the upstream
# server is probably only accessible within the VPC.
# Note also we are also asking nginx to replacing the URI, so that
# requests to /downloads/my_file.zip will be routed to
# http://192.168.0.1/files/my_file.zip.
proxy_pass http://192.168.0.1/files;
}
}
}
# vim: ft=nginx
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment