Magento Forum

   
Page 1 of 3
NginX Web Server with Magento
 
nysis
Jr. Member
 
Total Posts:  1
Joined:  2008-05-10
 

Have anyone any information about using Magento with nginx web server?
Any Howtos available? Thank you

 
Magento Community Magento Community
Magento Community
Magento Community
 
laurent
Member
 
Total Posts:  75
Joined:  2007-08-31
Paris, France
 

I got Magento running with Nginx on my development server.
Here is my quick & dirty Nginx configuration file (not intended for production use!):

server {
    listen   80
;
    
server_name  magento;

    
access_log  /var/log/nginx/magento.access.log;
    
error_log  /var/log/nginx/magento.error.log;
    
    
gzip on;
    
#gzip_comp_level 9;
    
gzip_min_length  1000;
    
gzip_proxied any;
    
gzip_types       text/plain application/xml text/html text/css text/js application/x-javascript;
    
    
#error_page  404  /404.html;

    # redirect server error pages to the static page /50x.html
    
error_page   500 502 503 504  /50x.html;
    
location = /50x.html {
        root   
/var/www/nginx-default;
    
}

    
# media
    
location ~* \.(png|gif|jpg|jpeg|css|js|swf|ico)$ {
        root   
/home/laurent/sites/magento-1.0/;
        
access_log off;
        
expires 30d;
    
}

    
# pass the PHP scripts to FastCGI socket
    
location \.php{
        fastcgi_pass   unix
:/tmp/php-fcgi.sock;
        
fastcgi_index index.php;
        
fastcgi_param  SCRIPT_NAME $fastcgi_script_name;
        
fastcgi_param  SCRIPT_FILENAME /home/laurent/sites/magento-1.0$fastcgi_script_name;
        include /
etc/nginx/fastcgi_params;
    
}
    location 
^~ /js/proxy.php {
        fastcgi_pass   unix
:/tmp/php-fcgi.sock;
        
fastcgi_index index.php;
        
fastcgi_param  SCRIPT_NAME $fastcgi_script_name;
        
fastcgi_param  SCRIPT_FILENAME  /home/laurent/sites/magento-1.0$fastcgi_script_name;
        include /
etc/nginx/fastcgi_params;
        
access_log off;
        
expires 30d;
    
}
    location 
{
        fastcgi_pass   unix
:/tmp/php-fcgi.sock;
        
fastcgi_index index.php;
        
fastcgi_param  SCRIPT_NAME index.php;
        
fastcgi_param  SCRIPT_FILENAME /home/laurent/sites/magento-1.0/index.php;
        include /
etc/nginx/fastcgi_params;
    
}
}

 
Magento Community Magento Community
Magento Community
Magento Community
 
DaveAllen
Jr. Member
 
Total Posts:  3
Joined:  2008-06-02
 

Just wondering if anyone else has had success with NGINX and Magento?

Since nginx does not use .htaccess files I am curious to how this will work for SCO (Proper URL formatting) under Magento?

 
Magento Community Magento Community
Magento Community
Magento Community
 
revelnick
Jr. Member
 
Total Posts:  11
Joined:  2008-09-02
 

I’m going to try running magento with nginx and php as fast_cgi.  It looks like the bulk of the URL rewriting in magento is done with zend’s routes.  I couldn’t find the keyword URLs stored anywhere but in the database so maybe i can get rewriting working using just the nginx rewrite module.

Ping me if you’re trying something similar.

Nick

 
Magento Community Magento Community
Magento Community
Magento Community
 
revelnick
Jr. Member
 
Total Posts:  11
Joined:  2008-09-02
 

take a look at this thread regarding nginx and zend/magento::

http://www.ruby-forum.com/topic/130700

location  /   {
          fastcgi_pass   [insert address 
for php];
          
fastcgi_param  SCRIPT_FILENAME [insert path to magento root];
          
[include whatever else];
      
}
location 
"\.(js|ico|gif|jpg|png|css)$" {
          root    [insert path to magento root]
;
}

this says send everything to php except static files.

the SEO urls still work.

 
Magento Community Magento Community
Magento Community
Magento Community
 
cff
Jr. Member
 
Total Posts:  27
Joined:  2008-08-29
 

This all seems to work perfectly for the frontend, but I can’t log into the backend - it just redirects back to the login form after filling it in. Does anyone have any ideas why? The front end is 99% okay (a few small issues).

More importantly if anyone is thinking of using nginx I have set up NGINX with XCACHE and PHP-FPM, the site is well over 500% faster - I would whole heartedly recommend it to anyone. I was using a 1Gig VPS before and getting shocking performance (3 people on the site at once = 10 second plus response time) now it’s sub 1 second response times with 5 or more people on the site on a 256MB VPS.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Carumba
Jr. Member
 
Total Posts:  18
Joined:  2009-02-27
California
 

I think I have most of this working including access to the backend.

Without using php-fpm (yet), I’ve cut over a second off load times.

Just out of curiosity, has anyone experimented with using varnish as a reverse proxy cache front yet? (Sitting in front of nginx)

 
Magento Community Magento Community
Magento Community
Magento Community
 
uke_boy
Jr. Member
 
Total Posts:  26
Joined:  2009-02-20
Kiev, Ukraine
 

Hi, guys!
Here is config for Nginx, based on this:
h__p://varien.com.ua/blog/ustanovka-magento-dlya-raboty-s-nginx-php-fpm-apc/.

It works pretty well, with only one exception: when loading product management page in Admin (not the grid, but individual product), browser says: “Error: can’t load Flex Library”.  Library located in js/lib/flex.js

Every other things, such as admin, downloader, SEO rewrites etc is OK.

user              nginx;
worker_processes  1;
error_log         /var/log/nginx/error.log;
pid               /var/run/nginx.pid;

events {
    worker_connections  1024
;
}

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;
    
tcp_nodelay on
    
    
client_header_timeout  3m;
        
client_body_timeout    3m;
        
send_timeout           3m;

        
client_header_buffer_size    1k;
        
large_client_header_buffers  4 4k;

    
output_buffers   1 32k;
        
postpone_output  1460;


        
keepalive_timeout  60 15;

    
# fastcgi nodes
    
upstream  backend  {
        server unix
:/tmp/php-fastcgi.socket;
    
}

    server {
        listen       80
;
        
server_name  www.mobilizer.com.ua mobilizer.com.ua;
        
root   /var/www/html/;
        
autoindex off;

        
gzip on# use gzip compression
     
gzip_min_length 1100;
     
gzip_buffers 4 8k
        
gzip_proxied any# enable proxy for the fcgi requests
        
gzip_types text/plain text/css application/x-javascript text/javascript application/json

        
# protection (we have no .htaccess)
        
location ~ (/(app/|includes/|lib/|/pkginfo/|var/|report/config.xml)|/\.svn/|/.hta.+) {
            deny all
;
        
}

        
# handle all .php files, /downloader and /report
        
location ~ (\.php|/downloader/?|/report/?)$ {
            
if ($request_uri ~ /(downloader|report)$)# no trailing /, redirecting
                
rewrite  ^(.*)$ $1permanent;
            
}
            fastcgi_index index
.php;
            include /
etc/nginx/fastcgi_params;
            
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            if (-
e $request_filename# check if requested path exists
                
fastcgi_pass backend;
            
}

        }

        
# handle magento
        
location /
        
{
            
# set expire headers
            
if ($request_uri ~* "\.(ico|css|js|gif|jpe?g|png)$"{
                expires max
;
            
}
            
# set fastcgi settings, not allowed in the "if" block
            
include /etc/nginx/fastcgi_params;
            
fastcgi_param SCRIPT_FILENAME $document_root/index.php;
            
fastcgi_param SCRIPT_NAME /index.php;
            
# rewrite - if file not found, pass it to the backend
            
if (!-f $request_filename{
                fastcgi_pass backend
;
                break;
            
}
        }
    }
}

I trying to run Fooman Speedster now and later shall post the result here.

 
Magento Community Magento Community
Magento Community
Magento Community
 
thaddeusmt
Member
 
Avatar
Total Posts:  33
Joined:  2009-02-02
Bozeman, MT
 

@uke_boy
any luck getting Speedster running? I am not having much luck.
thanks!

 
Magento Community Magento Community
Magento Community
Magento Community
 
liamstask
Jr. Member
 
Total Posts:  16
Joined:  2008-09-04
 

Anybody have any tips for running this setup at a non-root location ie, www.mysite.com/magento?  I haven’t for the life of me been able to figure out where I need to make adjustments for this to work.  If I serve magento up as the root, as shown here, all is well.  Thanks!

 
Magento Community Magento Community
Magento Community
Magento Community
 
clockworkgeek
Sr. Member
 
Avatar
Total Posts:  79
Joined:  2009-07-31
 

I thought I would share my config with you all, most is lifted from too many other sites to credit. My entire site works flawlessly (after a few last minute edits!).

EDIT. The following is now also a wiki article. If you have suggestions update that instead.

/etc/nginx/nginx.conf

user              nginx;
worker_processes  1;
error_log         /var/log/nginx/error.log;
pid               /var/run/nginx.pid;

events {
    worker_connections  1024
;
}

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;
    
autoindex off;
    
map $scheme $fastcgi_https { ## Detect when HTTPS is used
        
default off;
        
https on;
    
}

    keepalive_timeout  10
;

    
gzip  on;
    
gzip_comp_level 2;
    
gzip_proxied any;
    
gzip_types      text/plain text/html text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;

    
# Load config files from the /etc/nginx/conf.d directory
    
include /etc/nginx/conf.d/*.conf;

}

/etc/nginx/conf.d/DOMAIN.conf

server {
    listen 80
;
    
server_name DOMAIN.com;
    
rewrite $scheme://www.$host$request_uri permanent; ## Forcibly prepend a www
}

server {
    listen 80 
default;
    
server_name www.DOMAIN.com *.DOMAIN.com## Domain is here twice so server_name_in_redirect will favour the www
    
root /var/www/vhosts/DOMAIN.com;
 
    
location {
        index index
.html index.php## Allow a static html file to be shown first
        
try_files $uri $uri/ @handler## If missing pass the URI to Magento's front handler
        
expires 30d## Assume all files are cachable
    
}
    location 
/minify## Needed for Fooman Speedster
        
rewrite ^/minify/([0-9]+)(/.*\.(js|css))$ /lib/minify/m.php?f=$2&d;=$1 last;
    
}

    
## These locations would be hidden by .htaccess normally
    
location /app/                { deny all}
    location 
/includes/           { deny all}
    location 
/lib/                { deny all}
    location 
/lib/minify/         { allow all}  ## Deny is applied after rewrites so must specifically allow minify
    
location /media/downloadable{ deny all}
    location 
/pkginfo/            { deny all}
    location 
/report/config.xml   { deny all}
    location 
/var/                { deny all}

    location 
/var/export## Allow admins only to view export folder
        
auth_basic           "Restricted"## Message shown in login window
        
auth_basic_user_file htpasswd## See /etc/nginx/htpassword
        
autoindex            on;
    
}

    location  
/. ## Disable .htaccess and other hidden files
        
return 404;
    
}

    location 
@handler { ## Magento uses a common front handler
        
rewrite / /index.php;
    
}

    location 
\.php## Forward paths like /js/index.php/x.js to relevant handler
        
rewrite ^(.*\.php)/ $1 last;
    
}

    location 
\.php## Execute PHP scripts
        
expires        off## Do not cache dynamic content
        
fastcgi_pass   127.0.0.1:9000;
        
fastcgi_param  HTTPS $fastcgi_https;
        
fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        
fastcgi_params## See /etc/nginx/fastcgi_params
    
}
}

A few points about it all:
- Replace instances of DOMAIN with your domain, obviously.
- Similarly correct paths appropriate to your system.
- Duplicate the second file for each site and it’s secure equivalent. Newer versions of nginx can combine it in one ‘server’ block with the declaration ‘listen 443 ssl’.
- There are many performance tweaks that can be made which I’ve deliberately left out for simplicity.
- I prefer using ‘location’ blocks where possible. The performance benefits are negligible but I think many nested blocks look messy. Rewrite rules are for Apache!
- The excellent Fooman Speedster is catered for.
- When installed by yum nginx came with a fastcgi_params file that really helps.
- The /var/export/ folder is accessible for admins. To set up the access use the apache htpasswd utility like this:

htpasswd -/etc/nginx/htpasswd USERNAME
Choose a suitable username and when prompted, a password too.

 
Magento Community Magento Community
Magento Community
Magento Community
 
demonkoryu
Guru
 
Avatar
Total Posts:  328
Joined:  2008-11-26
Dortmund, Germany
 
clockworkgeek - 06 February 2010 09:09 AM

I thought I would share my config with you all, most is lifted from too many other sites to credit. My entire site works flawlessly (after a few last minute edits!).

Absolutely awesome. Thanks for sharing this!

BTW, this works (with a few minor edits) also for Typo3 sites.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Turnkeye
Enthusiast
 
Avatar
Total Posts:  908
Joined:  2008-12-20
URL: turnkeye.com
 

You can choose our Magento optimized NginX based hosting http://nginxhosting.com

 
Magento Community Magento Community
Magento Community
Magento Community
 
baten
Jr. Member
 
Avatar
Total Posts:  5
Joined:  2010-02-26
 

@clockworkgeek:

Thank you very very much for your detailled and good description.

It is working now: Magento with NGINX and SSL on Amazon EC2 infrastructure. Your config part:

fastcgi_param HTTPS $fastcgi_https;

solved my last problem which resulted in a redirect loop before.

Greets,
baten

 
Magento Community Magento Community
Magento Community
Magento Community
 
Turnkeye
Enthusiast
 
Avatar
Total Posts:  908
Joined:  2008-12-20
URL: turnkeye.com
 

BTW, have you noticed that MagentoCommerce.com switched to NginX?

 
Magento Community Magento Community
Magento Community
Magento Community
 
samm
Jr. Member
 
Total Posts:  29
Joined:  2009-04-02
 

A long time ago btw. Now we are running on latest stable with some local modifications.

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
    Back to top
Page 1 of 3