Reading list Switch to dark mode

    Magento with NGINX

    Updated 7 December 2015

    In our previous blog about nginx Server, we have described why nginx is better than apache due to its event driven approach and how to setup nginx for basic php use but in this article we will help you to configure nginx in more practical way so that you can run various frameworks like Magento, Joomla, Prestashop etc on nginx without any trouble.

    First of all its very important to understand the difference between apache and nginx, nginx does not support the .htaccess file. So due to this you have to write down all the rules in nginx vhost configuration file which is also depends on the framework.

    Lets take an example to run Magento with NGINX.

    • Create a vhost or site configuration file in “/etc/nginx/conf.d/DOMAIN.conf” and copy the following content into it.
    server {
    
    listen 80;
    
    server_name DOMAIN.com;
    
    #rewrite / $scheme://www.$host$request_uri permanent; ## Forcibly prepend a www
    
    }
    
    server {
    
    listen 80 default;
    
    ## SSL directives might go here
    
    server_name DOMAIN.com *.DOMAIN.com; ## Domain is here twice so server_name_in_redirect will favour the www
    
    root /var/www/; //Document root of your website
    
    location / {
    
    index index.php index.html; ## Allow a php 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
    
    }
    
    ## These locations would be hidden by .htaccess normally
    
    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; }
    
    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
    
    if (!-e $request_filename) { rewrite / /index.php last; } ## Catch 404s that try_files miss
    
    expires off; ## Do not cache dynamic content
    
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    
    fastcgi_param MAGE_RUN_CODE default; ## Store code is defined in administration > Configuration > Manage Stores
    
    fastcgi_param MAGE_RUN_TYPE store;
    
    include fastcgi_params; ## See /etc/nginx/fastcgi_params
    
    }
    
    location ~ .php/ { ## Forward paths like /js/index.php/x.js to relevant handler
    
    rewrite ^(.*.php)/ $1 last;
    
    }
    
    location ~ .php$ { ## Execute PHP scripts
    
    if (!-e $request_filename) { rewrite / /index.php last; } ## Catch 404s that try_files miss
    
    expires off; ## Do not cache dynamic content
    
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    
    fastcgi_param MAGE_RUN_CODE default; ## Store code is defined in administration > Configuration > Manage Stores
    
    fastcgi_param MAGE_RUN_TYPE store;
    
    include fastcgi_params; ## See /etc/nginx/fastcgi_params
    
    }
    
    }
    Note -: you have to remove the "/etc/nginx/sites-available/default" or
    change the server port in the default file to prevent the port conflict.
    • Edit the DOMAIN.com with your website domain and make sure that you have entered the correct document root of your website.
    • Unzip magento files in /var/www/ folder according to this configuration.
    • Restart your nginx and php5-fpm.

    After that you will be able to run Magento Store on nginx server which is more efficient as compared on apache.

     

    Start your headless eCommerce
    now.
    Find out More
    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home