Serving with nginx
From Surebert wiki
Contents |
Overview
You can server sbf framework projects with nginx instead of apache. These instructions assume centos
Install nginx and lighttp from yum
bash code
yum --enablerepo=epel install nginx yum --enablerepo=dag install lighttpd-fastcgi
Spawn fcgi processes for nginx to use
bash code
/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u apache -g apache -f /usr/bin/php-cgi -P /var/run/fastcgi-php.pid
You can add this to rc.local in order to start at reboot
To kill those same processes use
bash code
kill $(cat /var/run/fastcgi-php.pid)
text code
#######################################################################
#
# This is the main Nginx configuration file.
#
# More information about the configuration options is available on
# * the English wiki - http://wiki.codemongers.com/Main
# * the Russian documentation - http://sysoev.ru/nginx/
#
#######################################################################
#----------------------------------------------------------------------
# Main Module - directives that cover basic functionality
#
# http://wiki.codemongers.com/NginxMainModule
#
#----------------------------------------------------------------------
user apache;
worker_processes 5;
error_log /var/log/nginx/error.log;
#error_log /var/log/nginx/error.log notice;
#error_log /var/log/nginx/error.log info;
pid /var/run/nginx.pid;
#----------------------------------------------------------------------
# Events Module
#
# http://wiki.codemongers.com/NginxEventsModule
#
#----------------------------------------------------------------------
events {
worker_connections 1024;
}
#----------------------------------------------------------------------
# HTTP Core Module
#
# http://wiki.codemongers.com/NginxHttpCoreModule
#
#----------------------------------------------------------------------
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] $request '
'"$status" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
gzip on;
# Load config files from the /etc/nginx/conf.d directory
include /etc/nginx/conf.d/*.conf;
#
# The default server
#
server {
listen 8000;
server_name yoursite.org;
access_log /var/log/nginx/yoursite.org.access.log;
error_log /var/log/nginx/yoursite.org.error.log;
set $root /var/www/html/yoursite.org/trunk/public;
root $root;
location / {
root $root;
index index.php index.html index.htm;
if (-f $request_filename) { expires max; break; }
if (!-e $request_filename) {
rewrite ^/(.+)$ /gateway.php/$1 last;
break;
}
}
location ~ gateway.php {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index gateway.php;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $root/gateway.php;
include /etc/nginx/fastcgi_params;
break;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $root$fastcgi_script_name;
}
error_page 404 /404.html;
location = /404.html {
root /usr/share/nginx/html;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
}Using nginx and apache
When using both servers together you will need to make sure they share a session and cookies.
In order for this too work, edit you /private/config/definitions.php and add this before the session start. Then make sure to define all cookies with set_cookie instead of php's setcookie. This will force all cookies to be readable by main host and subdomain.
php code
ini_set('session.name', 'SBF_ID'); ini_set('session.cookie_domain', COOKIE_DOMAIN); function set_cookie($name, $value, $expire='', $path=''){ $path = $path ? $path : COOKIE_DOMAIN; setcookie($name, $value, $expire, COOKIE_DOMAIN); }
nginx as proxy to apache
If you still have other sites on the box that require apache you can have nginx proxy those requests to apache by running apache on another port locally. The end user would have no idea.
First you need to make sure apache is running on port 8000 or some other port that does not conflict with nginx running on port 80 for this to work.
This is changed in /etc/httpd/httpd.conf
bash code
Listen 8000By adding the following to your nginx.conf you are proxying all requests that do not match the servername specified in the above block to local apache instance which running on port 8000.
bash code
server { listen 80 default; # # Proxy all remaining content to Apache # location / { proxy_pass http://127.0.0.1:8000/; } }
apache as proxy to nginx
make sure apache listens on 80 in httpd.conf
text code
Listen 80
as subdirectory
set virtual host to use nginx as proxy in httpd-vhosts.conf - in this example all requests for /nginx are redirected to nginx running on port 8000
text code
<VirtualHost *:80> DocumentRoot /var/www/html/wwwroot ServerName test.com ProxyPass /nginx http://localhost:8000/ ProxyPassReverse /nginx http://localhost:8000/ ProxyPreserveHost On ErrorLog logs/test.com-error_log CustomLog logs/test.com-access_log common </VirtualHost>
as subdomain
text code
<VirtualHost *:80> DocumentRoot /var/www/html/wwwroot ServerName test.com ErrorLog logs/test.com-error_log CustomLog logs/test.com-access_log common </VirtualHost> <VirtualHost *:80> DocumentRoot /var/www/html/wwwroot ServerName cdn.test.com ProxyPass / http://localhost:8000/ ProxyPassReverse / http://localhost:8000/ ProxyPreserveHost On ErrorLog logs/cdn.test.com-error_log CustomLog logs/cdn.test.com-access_log common </VirtualHost>
use the same nginx.conf as above but replace listen 80 with listen 8000
text code
server {
listen 8000;
server_name test.com;restart both apache and nginx
bash code
service httpd restart service nginx restart
You could also have it proxy for just a subdomain. This is useful when using it for a CDN or for delivering real time ajax as subdomains get download data in parallel with the main domain in the browser, helping you overcome the limited simultaneous connections per domain. Especially useful when using long polling AJAX or comet with nginx.