Skip to content

Instantly share code, notes, and snippets.

@thesabbir
Created October 24, 2016 17:56
Show Gist options
  • Save thesabbir/319f71dc53d88de2b4b50d0fbcf6f85c to your computer and use it in GitHub Desktop.
Save thesabbir/319f71dc53d88de2b4b50d0fbcf6f85c to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import argparse
from string import Template
"""
Subclassing template class
"""
class NginTemplate(Template):
delimiter = '#'
"""
Reverse Proxy Template
"""
reverse_proxy_template = """
server {
listen 80;
server_name #{server_name};
access_log /var/log/nginx/#{server_name}.access.log;
error_log /var/log/nginx/#{server_name}.error.log;
location / {
proxy_pass #{proxy_pass};
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}"""
"""
Initiate argparse
"""
parser = argparse.ArgumentParser()
"""
Add arguments
"""
parser.add_argument("-r", "--revproxy", help="reverse proxy", action="store_true")
parser.add_argument("-n", "--name", help="server name or domain name", action="store")
parser.add_argument("-p", "--proxypass", help="proxy pass server", action="store")
"""
Parsing arguments
"""
args = parser.parse_args()
"""
Reverse proxy config generator
Usage Example: ngin.py -r -n example.com -p http://localhost:9000
"""
if args.revproxy:
if args.name is None or args.proxypass is None:
raise SystemExit('Name and Pass is required!')
params = {'server_name': args.name, 'proxy_pass': args.proxypass}
conf = NginTemplate(reverse_proxy_template).safe_substitute(params)
print conf
@thesabbir
Copy link
Author

thesabbir commented Oct 24, 2016

output of ngin.py -r -n thesabbir.com -p http://localhost:7000 :

server {
    listen 80;
    server_name thesabbir.com;
    access_log /var/log/nginx/thesabbir.com.access.log;
    error_log /var/log/nginx/thesabbir.com.error.log;

    location  / {
        proxy_pass http://localhost:7000;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment