← Back
import re
import sys

def main():
    conf_path = '/etc/nginx/sites-available/options-api'
    print(f"Reading Nginx config from: {conf_path}")
    
    try:
        with open(conf_path, 'r') as f:
            config = f.read()

        # The new proxy block pointing to port 8080
        proxy_block = """location / {
        proxy_pass http://127.0.0.1:8080;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }"""
        
        # Replace location / { try_files ... } block with the new proxy block
        new_config = re.sub(r'location\s+/\s*\{[^}]*try_files[^}]*\}', proxy_block, config)
        
        # Also remove the root and index directives from the server block
        new_config = re.sub(r'\s+root\s+/home/[^;]+;', '', new_config)
        new_config = re.sub(r'\s+index\s+index\.html;', '', new_config)

        if config == new_config:
            print("No static rules found to replace, or config already updated.")
        else:
            with open(conf_path, 'w') as f:
                f.write(new_config)
            print("Successfully updated Nginx configuration.")
            
    except Exception as e:
        print(f"Failed to update Nginx configuration: {e}")
        sys.exit(1)

if __name__ == "__main__":
    main()

📜 Git History

bad0ef4fix(v2): safely edit specific options-api nginx conf using python script4 months ago
Show last diff
Loading...