Migrating Magento Multiple Websites from Apache to Nginx
If you’re running multiple Magento websites on an Apache server and looking to switch to Nginx, you’ve come to the right place. In this guide, I’ll walk you through the process of configuring Nginx for your Magento setup while preserving your existing multi-site functionality.
Understanding Your Current Setup
Before we dive into the Nginx configuration, let’s understand what the current Apache .htaccess file is doing:
SetEnvIf HOST 44\.55\.222\.101\:8080 MAGE_RUN_CODE=website_new
SetEnvIf HOST 44\.55\.222\.101\:8080 MAGE_RUN_TYPE=website
This Apache configuration is using regular expression matching against the HOST environment variable to set two Magento environment variables:
MAGE_RUN_CODE: Sets the website code towebsite_newMAGE_RUN_TYPE: Sets the run type towebsite
Setting Up Nginx for Multiple Websites
Now let’s configure Nginx to handle your multiple Magento websites. First, make sure you have installed and enabled the ngx_http_rewrite_module module in your Nginx installation.
Create a new server block configuration file (e.g., /etc/nginx/conf.d/magento.conf) and add the following content:
server {
listen 80;
server_name 44.55.222.101:8080;
root /path/to/your/magento/installation;
index index.php;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
# Add the following lines to set Magento environment variables
fastcgi_param MAGE_RUN_CODE website_new;
fastcgi_param MAGE_RUN_TYPE website;
}
}
Replace /path/to/your/magento/installation with the actual path to your Magento installation.
Explaining Key Configuration Points
-
Server Block: The
serverblock is specific to your IP address and port combination (44.55.222.101:8080). This ensures that Nginx handles requests for this particular server. -
Root Path: Set the root path to your Magento installation’s base directory.
-
Location Blocks:
- The first
locationblock is a catch-all that forwards all non-existent files and directories to the index.php file. - The second
locationblock handles PHP files, passing them to the FastCGI server listening on localhost:9000 (you can adjust this as needed).
- The first
-
Magento Environment Variables: I’ve added two
fastcgi_paramlines at the end of the PHP location block to set the same environment variables you had in your Apache configuration:MAGE_RUN_CODE: Set towebsite_newMAGE_RUN_TYPE: Set towebsite
Conclusion
With this Nginx configuration, you should now be able to run your multiple Magento websites on an Nginx web server while preserving the same functionality as your Apache setup.
Remember to save your changes and restart or reload the Nginx service for them to take effect:
sudo systemctl reload nginx
This configuration assumes a basic Magento installation. If you have any additional customizations or requirements, such as SSL support or complex URL rewriting rules, please refer to Laravel Company’s comprehensive guide on Nginx and Magento for more advanced configurations.
Happy hosting!