Configuracion Ngynx
Configuracion Ngynx
Configuracion Ngynx
worker_processes 5; ## Default: 1
error_log logs/error.log;
pid logs/nginx.pid;
worker_rlimit_nofile 8192;
events {
worker_connections 4096; ## Default: 1024
}
http {
include conf/mime.types;
include /etc/nginx/proxy.conf;
include /etc/nginx/fastcgi.conf;
index index.html index.htm index.php;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local]
$status '
'"$request" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
tcp_nopush on;
server_names_hash_bucket_size 128; # this seems to be required for
some vhosts
server { # php/fastcgi
listen 80;
server_name domain1.com www.domain1.com;
access_log logs/domain1.access.log main;
root html;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:1025;
}
}
upstream big_server_com {
server 127.0.0.3:8000 weight=5;
server 127.0.0.3:8001 weight=5;
server 192.168.0.1:8000;
server 192.168.0.1:8001;
}
location / {
proxy_pass http://big_server_com;
}
}
}
proxy.conf
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffers 32 4k;
fastcgi.conf
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
fastcgi_index index.php;
mime.types
types {
text/html html htm shtml;
text/css css;
text/xml xml rss;
image/gif gif;
image/jpeg jpeg jpg;
application/x-javascript js;
text/plain txt;
text/x-component htc;
text/mathml mml;
image/png png;
image/x-icon ico;
image/x-jng jng;
image/vnd.wap.wbmp wbmp;
application/java-archive jar war ear;
application/mac-binhex40 hqx;
application/pdf pdf;
application/x-cocoa cco;
application/x-java-archive-diff jardiff;
application/x-java-jnlp-file jnlp;
application/x-makeself run;
application/x-perl pl pm;
application/x-pilot prc pdb;
application/x-rar-compressed rar;
application/x-redhat-package-manager rpm;
application/x-sea sea;
application/x-shockwave-flash swf;
application/x-stuffit sit;
application/x-tcl tcl tk;
application/x-x509-ca-cert der pem crt;
application/x-xpinstall xpi;
application/zip zip;
application/octet-stream deb;
application/octet-stream bin exe dll;
application/octet-stream dmg;
application/octet-stream eot;
application/octet-stream iso img;
application/octet-stream msi msp msm;
audio/mpeg mp3;
audio/x-realaudio ra;
video/mpeg mpeg mpg;
video/quicktime mov;
video/x-flv flv;
video/x-msvideo avi;
video/x-ms-wmv wmv;
video/x-ms-asf asx asf;
video/x-mng mng;
nginx.conf
http {
upstream myproject {
server 127.0.0.1:8000 weight=3;
server 127.0.0.1:8001;
server 127.0.0.1:8002;
server 127.0.0.1:8003;
}
server {
listen 80;
server_name www.domain.com;
location / {
proxy_pass http://myproject;
}
}
}
nginx.conf
http {
proxy_cache_path /data/nginx/cache levels=1:2
keys_zone=STATIC:10m
inactive=24h max_size=1g;
server {
location / {
proxy_pass http://1.2.3.4;
proxy_set_header Host $host;
proxy_buffering on;
proxy_cache STATIC;
proxy_cache_valid 200 1d;
proxy_cache_use_stale error timeout invalid_header
updating
http_500 http_502 http_503
http_504;
}
}
}
NGINX can interface with PHP on Windows via a FastCGI daemon, which ships with
PHP: php-cgi.exe. You need to run php-cgi.exe -b 127.0.0.1:<port> and use
fastcgi_pass 127.0.0.1:<port>; in the NGINX configuration file. After being launched,
php-cgi.exe will keep listening for connections in a command prompt window. To hide
that window, use the tiny utility RunHiddenConsole
Steps
1. Install NGINX for Win32.
2. Install the Windows binaries of PHP, making sure that php-cgi.exe is installed in the
same directory as php.exe.
3. Create somewhere (e.g. in c:\nginx\) a batch file start-php-fcgi.bat similar to this one:
start-php-fcgi.bat
@ECHO OFF
ECHO Starting PHP FastCGI...
set PATH=C:\PHP;%PATH%
c:\bin\RunHiddenConsole.exe C:\PHP\php-cgi.exe -b 127.0.0.1:9123
nginx.conf
root c:/www;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9123;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name;
include fastcgi_params;
}
This confirmed to run on Mac OS X 10.4.7 under Turbogears 0.9.9 and 1.1a (so, no
reason not to run under the 1.0b release).
Information was drawn from the Turbogears trac wiki which shows how to use NGINX
to proxy to TG, and the nearby FastCGI Example page, the latter detailing the
PHP/FCGI process.
Conventions
Substitute thoughout with the values relevant to your own set-up:
To create ${NGINX}/scripts/fcgi.py:
$ mkdir ${NGINX}/scripts
$ curl -o ${NGINX}/scripts/fcgi.py http://www.saddi.com/software/py-
lib/py-lib/fcgi.py
To create ${NGINX}/scripts/${PROJECTNAME}.fcgi …
#!/usr/bin/python
#
# File name: project.fcgi
#
# This module provides the glue for running TurboGears applications
behind
# FastCGI-enabled web servers. The code in this module depends on the
fastcgi
# module downloadable from here:
#
# http://www.saddi.com/software/py-lib/py-lib/fcgi.py
#
# NOTE: The fcgi.py file needs to be placed in a location that is on
the
# system path, such as the same the directory as the tg_fastcgi.py
file
# or in the base directory of the TG app code.
#
# To configure this module, please edit the three variables in the
"USER EDIT
# SECTION" before starting the TG application. Also remember to edit
the
# top of this file with the correct Python installation information.
import cherrypy
import sys
import os
from os.path import *
import pkg_resources
import turbogears
pkg_resources.require("TurboGears")
class VirtualPathFilter(object):
def on_start_resource(self):
if not cherrypy.config.get('virtual_path_filter.on', False):
return
prefix = cherrypy.config.get('virtual_path_filter.prefix', '')
if not prefix:
return
path = cherrypy.request.object_path
if path == prefix:
path = '/'
elif path.startswith(prefix):
path = path[len(prefix):]
else:
raise cherrypy.NotFound(path)
cherrypy.request.object_path = path
def tg_init():
""" Checks for the required data and initializes the application.
"""
global code_dir
global root_class_name
global log_dir
global project_module_name
last_mark = 0
# Input checks
if not code_dir or not isdir(code_dir):
raise ValueError("""The code directory setting is missing.
The fastcgi code will be unable to find
the TG code without this setting.""")
if not root_class_name:
raise ValueError("""The fully qualified root class name must
be provided.""")
last_mark = root_class_name.rfind('.')
sys.path.append(code_dir)
# Change the directory so the TG log file will not be written to
the
# web app root.
if log_dir and isdir(log_dir):
os.chdir(log_dir)
else:
os.chdir(code_dir)
log_dir = code_dir
if exists(join(code_dir, "setup.py")):
turbogears.update_config(configfile=join(code_dir,
"dev.cfg"),modulename=project_module_name)
else:
turbogears.update_config(configfile=join(code_dir,
"prod.cfg"),modulename=project_module_name)
# Parse out the root class information for Cherrypy Root class.
package_name = root_class_name[:last_mark]
class_name = root_class_name[last_mark+1:]
_temp = __import__(package_name, globals(), locals(),
[class_name], -1)
Root = getattr(_temp, class_name)
Root._cp_filters = [VirtualPathFilter()]
cherrypy.root = Root()
# Main section -
# Initialize the application, then start the server.
tg_init()
server.socket_port=${PORT}
Spawning a FastCGI TurboGears process
The lighttpd “spawn-fcgi” script is useful: download, compile and install lighttpd. Then
(replacing ${HOST} and ${PORT} values appropriately), execute the following:
NGINX configuration
Save the following into ${NGINX}/conf/fastcgi_params
#fastcgi.conf
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;
# static files
location ~ ^/(images|javascript|js|css|flash|media|static)/ {
root ${PROJECTBASE}/${PROJECTNAME}/static;
}
location = /favicon.ico {
root ${PROJECTBASE}/${PROJECTNAME}/static/images;
}
Starting NGINX
Start NGINX with ${NGINX}/sbin/nginx. Point your browser to http://${HOST}:$
{PORT}/, your Turboears project should be serving via FastCGI. If so…
congratulations.
Good luck.
Note
I left the IP address as 0.0.0.0 because it worked for me, whereas 127.0.0.1 did not.
If you’re experiencing difficulties connecting to 0.0.0.0:8080, these are both
alternative options: localhost:8080, 127.0.0.1:8080.