How To Configure Magento On Lightweight Ningx Web Server

Apache in itself is big and consume too much memory and resources. Ningx web server on the other hand doesn’t consume resources as Apache and somewhat less resource crazy. Running Magento powered eCommerce on nginx have several speed specific advantages but configuring ngnix for Magento is not as easy as configuring Magento on Apache.

Believe me or not but Magento is a resource hungry application and when the traffic or bot comes, it puts immense load on Apache server (running on low RAM), in such scenarios all you can do is to restart your Apache to resume normal operation. Fortunately, ngnix will save you from restarting the server every time the load is too high. In the following configuration i am making sure that your Magento installation runs fine on http and https both. Now, lets add SSL support in your nginx.conf file


##
# SSL Support
##

map $scheme $fastcgi_https {
default off;
https on;
}

Now, lets create a file which will define how many sites will be hosted on nginX (Virtual host file for Apache world). Please change mydomain.com in the following code along with the server path and SSL certificate files/keys.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
server {
    # Listen on port 80 as well as post 443 for SSL connections.
    listen 80;
    listen 443 default ssl;
 
    server_name mydomain.com www.mydomain.com;
 
    # Specify path to your SSL certificates.
    ssl_certificate /etc/nginx/certificates/yourcertificate.crt;
    ssl_certificate_key /etc/nginx/certificates/yourcertificate.key;
 
    # Path to the files in which you wish to
    # store your access and error logs.
    access_log /path/to/your/logs/access_log;
    error_log /path/to/your/logs/error_log;
 
    # If the site is accessed via mydomain.com
    # automatically redirect to www.mydomain.com.
    if ($host = 'mydomain.com' ) {
        rewrite ^/(.*)$ http://www.mydomain.com/$1permanent;
    }
 
    root /path/to/magento;
 
    location / {
        index index.html index.php;
        try_files $uri $uri/ @handler;
    }
 
    # Deny access to specific directories no one
    # in particular needs access to anyways.
    location /app/ { deny all; }
    location /includes/ { deny all; }
    location /lib/ { deny all; }
    location /media/downloadable/ { deny all; }
    location /pkginfo/ { deny all; }
    location /report/config.xml { deny all; }
    location /var/ { deny all; }
 
    # Allow only those who have a login name and password
    # to view the export folder. Refer to /etc/nginx/htpassword.
    location /var/export/ {
        auth_basic "Restricted";
        auth_basic_user_file htpasswd;
        autoindex on;
    }
 
    # Deny all attempts to access hidden files
    # such as .htaccess, .htpasswd, etc...
    location ~ /\. {
         deny all;
         access_log off;
         log_not_found off;
    }
 
    # This redirect is added so to use Magentos
    # common front handler when handling incoming URLs.
    location @handler {
        rewrite / /index.php;
    }
 
    # Forward paths such as /js/index.php/x.js
    # to their relevant handler.
    location ~ .php/ {
        rewrite ^(.*.php)/ $1 last;
    }
 
    # Handle the exectution of .php files.
    location ~ .php$ {
        if (!-e $request_filename) {
            rewrite / /index.php last;
        }
        expires off;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param HTTPS $fastcgi_https;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param MAGE_RUN_CODE default;
        fastcgi_param MAGE_RUN_TYPE store;
        include fastcgi_params;
    }
}

Save this configuration file (nginx.conf) then create a symbolic link for this file in your site-available folder.

ln -s /path/to/nginx/sites-available/mydomain.com /path/to/nginx/sites-enabled/mydomain.com

We are all done, final task is to test both http and https versions and making sure that the site is opening fine for SSL and non SSL requests.

Let me know if you run into any trouble using nginX as a http server for Magento. Subscribe our RSS to receive latest Magento updates.