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.
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.
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;
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 ^(.*)$ $1/ permanent; } 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.
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!
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;
# 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 -c /etc/nginx/htpasswd USERNAME
Choose a suitable username and when prompted, a password too.
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.