Python + Nginx + fcgi on Debian 5.0 Lenny

Once we've set up fcgi on nginx following the instructions for running Nagios on nginx, we can add Python support with a just a few modifications.

Let's say we want to run a custom application written in Python. Let's say the application sends us reminders of Nagios alerts we have acknowledged but not fixed and provides us a link on the Nagios server that we can click to update our plans for fixing the alert. All we need do is install the application's Python scripts in, say, /usr/lib/cgi-bin/rem/ and add a handful of lines to our already-modified server file /etc/nginx/sites-enabled/default to let nginx know how to handle the incoming python status update.

If the reminder application also has static files, we would create a directory in which they could reside. In this case, we will leave them out but leave the Nagios3 web-configuration stanzas in-place so that you can see compare the lines we added for the Nagios3 application with those we added to provide Python support.

/etc/nginx/sites-enabled/default
server {
        server_name    nagios.demo.com
        listen 80;
        add_header      Cache-Control public;
        access_log      /var/log/nginx/nagios.demo.com_access.log;
        error_log       /var/log/nginx/nagios.demo.com_error.log info;
        expires         31d;
 
        location / {
            root /usr/share/nagios3/htdocs;
            index index.html;
 
            # A rewrite serves URI requests to the stylesheets
            rewrite ^/nagios3/stylesheets/(.*)$ /../stylesheets/$1 break;
            rewrite ^/nagios3/(.*)$ /$1 break;
        }
 
        location ~ \.cgi$ {
            root /usr/lib/cgi-bin/nagios3;
            include /etc/nginx/fastcgi_params;
 
            # Another rewrite serves URI requests to the cgi files
            rewrite ^/cgi-bin/nagios3/(.*)$ /$1;
 
            auth_basic "Restricted";
            auth_basic_user_file /etc/nagios3/htpasswd.users;
 
            fastcgi_pass 127.0.0.1:8998;
            fastcgi_param SCRIPT_FILENAME /usr/lib/cgi-bin/nagios3$fastcgi_script_name;
            fastcgi_param AUTH_USER     $remote_user;
            fastcgi_param REMOTE_USER   $remote_user;
        }
 
# Add in support for a python application
            location ~ \.py$ {
            root /usr/lib/cgi-bin/rem;
            include /etc/nginx/fastcgi_params;
 
            fastcgi_pass 127.0.0.1:8998;
            fastcgi_param SCRIPT_FILENAME /usr/lib/cgi-bin/rem$fastcgi_script_name;
        }
 
 
    } # end server

Discussion

Enter your comment
BYGER
 
pythononnginx.txt · Last modified: 2011/04/05 14:15 by lee
Translate: AR CN DE ES FR HI PT PT-br RU UR