Magento 2 NGINX Multi Website: Steps to Configure Multiple Websites
Looking to manage multiple stores efficiently while boosting performance? Magento 2 NGINX Multi Website is a setup for multiple websites. It allows you to configure these websites from one Magento installation. This setup boosts both speed and scalability.
This tutorial will cover how to set up Magento 2 with NGINX and manage multiple websites.
Key Takeaways
-
Web server software boosts the speed and scalability of several websites.
-
SEO tips help each website rank better and avoid issues.
-
Steps to set up NGINX Multi Website in Magento.
-
Easy tips improve performance and keep your websites fast.
-
Protect your websites with suitable SSL and security settings.
-
Troubleshooting common issues keeps your stores running smoothly.
What is Magento 2 NGINX Multi Website?
Magento 2 NGINX Multi Website manages multi-websites from one Magento 2 installation. Each website can have its own store view and base URL, but all are controlled in one Magento admin.
Magento 2 NGINX multi-website is highly efficient for businesses running multiple stores. It offers flexible options for SEO, performance optimization, and security. Using NGINX as the HTTP server, each website can have its own:
-
Unique domain
-
Design
-
Product catalog, but they all share the same backend.
6 Benefits of Using NGINX for Websites and Stores
1. High Performance
The nginx setup is lightweight and efficient. It handles large amounts of auto-scaling traffic while delivering fast responses. Unlike Apache, which can slow down under heavy loads and works well even with high demand. For example, when managing many websites in a setup. It ensures faster content delivery. It keeps your site or store responsive during peak times.
2. Scalability
The nginx configuration files are built to scale quickly. It uses an event-driven model that handles many connections at the same time. This works well for businesses running two websites or more from the same Magento root directory. As your eCommerce grows, you can scale your infrastructure without losing performance.
3. Reverse Proxy and Load Balancing
In a multi-store setup, HTTP server software acts as a reverse proxy. It distributes traffic to multiple backend servers. This is helpful when configuring the NGINX to handle multiple domains in one virtual environment. Large platforms like Shopify use this setup. They ensure high availability and balance server loads. Using server block. Server software spreads the traffic to reduce the risk of overload.
4. Caching
HTTP server software has built-in caching features. It stores frequently accessed content, reducing the load on your servers. For example, you can enable caching in your nginx server block file to speed up product page delivery. This reduces the need for repeated calls to the php entry point for main content. It also improves load times for your websites on the storefront.
5. SSL/TLS Termination
The server makes SSL/TLS termination easy and secures communication between clients and servers. By offloading SSL decryption from the backend, it lightens the load on root directory. This is important when managing multiple domains, as each domain needs SSL. Companies use SSL termination to protect sensitive data without overloading their servers.
6. URL Rewriting
Server software offers powerful URL rewriting capabilities. It lets you create clean, SEO-friendly URLs. For example, instead of example .com/product.php?id=123, you can rewrite it to example .com/product/shoes. It improves SEO and user experience. For each website or store, you can add the store code to make URLs unique and structured.
SEO Considerations for Magento NGINX Multi Website Setup
Aspects | Details |
---|---|
Canonicalization | Use canonical tags to prevent duplicate content. Set either the www or non-www version of your default store as the primary domain. It helps to avoid duplication issues on websites. |
Content Optimization | Ensure each website or store view has unique content and improve SEO. Make sure all websites are mobile-friendly for better rankings and user experience. |
Root URL Configuration | Update the root URL for each website or store view in store. It ensures the correct domain. Maintain a consistent URL structure across domains in one virtual host. |
SSL Configuration | Secure each domain in your one virtual host file with an SSL certificate. It helps protect data and improves users' trust in the search engine. |
Sitemap Management | Create a tiered XML sitemap that breaks categories into smaller sections. Add the sitemap to Google Webmaster Tools and include it in the htaccess file. It helps in proper indexing for the websites. |
Cross-Origin Resource Sharing | Properly set up CORS to prevent cross-origin errors. It is done when loading static content from multiple domains in one setup. |
Benefits of MGT’s NGINX-Optimized Web Server
MGT already has an NGINX-optimized web server. It is built to enhance the performance and efficiency of your Magento store. With the advanced technology, your site will run smoothly, even during high traffic. The key benefits are as follows:
1. Low Memory Usage
MGT’s NGINX server uses an event-driven architecture. It does not need to create new processes for each request. This helps reduce memory usage and ensures that your Magento store runs smoothly. It works well even when there’s heavy traffic on the website.
2. gRPC Support
MGT’s NGINX server includes support for gRPC, a modern way for clients and servers to communicate. It helps send and receive data quickly. This makes your ecommerce store more efficient and responsive.
3. WebSocket Support
With WebSocket support in your server, your online store can offer real-time, two-way communication. It is between the users and the server. This is perfect for features like live chat or instant updates on your site.
4. FastCGI Support
MGT’s NGINX server supports FastCGI, which boosts your ecommerce store’s performance. FastCGI processes many requests without needing to start a new process each time. It helps your online store load faster.
5. Handling Static Files and Auto-indexing
MGT’s NGINX server is designed to serve static content, like images or files, at high speed. It can also handle index files automatically. This makes file management simpler and faster for your Magento store.
6. TLS/SSL with SNI
NGINX server includes SNI in the TLS/SSL handshake process. This ensures the correct SSL certificate is used for your online site. It secures the connection and protects customer data.
7. Reverse Proxy with Caching
The server acts as a reverse proxy, which means it can direct traffic and cache content. This speeds up your Magento store by reducing the time it takes to load pages for your visitors.
8. URL Redirection
The server allows easy URL rewriting and redirection. It helps direct visitors from one page to another smoothly. It also improves the user experience of your online store.
9. IP-based and Name-based Virtual Hosting
NGINX server supports both IP-based and name-based virtual hosting. This means it can manage multiple domains and balance the traffic between them. It is ideal for handling large amounts of visitors to your online store.
4 Steps to Configure NGINX Multi Website in Magento
Step 1: Create Websites, Stores, and Store Views in Admin
-
Go to Stores > Settings > All Stores.
-
Click Create Website and enter details like the name and mage_run_code. Save the website.
-
Click Create Store, choose the website you created, and enter the store details. Save the store.
-
Click Create Store View, select the store, and enter the store view details. Save the store view.
Step 2: Create NGINX Server Hosts
-
Create a Server Host File (for one website):
-
Open a text editor and create a new file in
/etc/nginx/sites-available/magento.
-
Add the following configuration:
-
`map $http_host $MAGE_RUN_CODE {
default '';
french .mysite.mg french;
german .mysite.mg german;
}
server {
listen 80; server_name mysite .mg french .mysite.mg german.mysite.mg;
set $MAGE_ROOT /var/www/html/magento2;
set $MAGE_MODE developer;
set $MAGE_RUN_TYPE website;
include /var/www/html/magento2/nginx.conf;
}`.
- Save and exit the editor.
-
Verify Web Server Software Setup:
-
Run this command to check the configuration:
nginx -t.
-
If successful, you will see the following:
nginx: configuration file /etc/nginx/nginx.conf test is successful
.
-
-
Create Symbolic Links:
-
Navigate to
/etc/nginx/sites-enabled
:cd /etc/nginx/sites-enabled
-
Create symbolic links to the server host file:
ln -s /etc/nginx/sites-available/magento magento.
-
-
Multiple Virtual Server Hosts:
- Open a text editor and create a new file
/etc/nginx/sites-available/french.mysite.mg
:
- Open a text editor and create a new file
`server {
listen 80; server_name french.mysite.mg;
set $MAGE_ROOT /var/www/html/magento2;
set $MAGE_MODE developer;
set $MAGE_RUN_TYPE store;
set $MAGE_RUN_CODE french;
include /var/www/html/magento2/nginx.conf;
}`.
- Create another file for the German website-
etc/nginx/sites-available/german.mysite.mg
:
`server {
listen 80; server_name german .mysite.mg;
set $MAGE_ROOT /var/www/html/magento2;
set $MAGE_MODE developer;
set $MAGE_RUN_TYPE store;
set $MAGE_RUN_CODE german;
include /var/www/html/magento2/nginx.conf;
}`.
- Save and exit the files.
-
Create Symbolic Links for Multiple Hosts:
- Navigate to
/etc/nginx/sites-enabled
and create symbolic links:
- Navigate to
`cd /etc/nginx/sites-enabled
ln -s /etc/nginx/sites-available/french.mysite.mg french .mysite.mg
ln -s /etc/nginx/sites-available/german.mysite.mg german.mysite.mg`.
Step 3: Modify `nginx.conf.sample`
-
Copy and Modify the File:
-
Do not edit the original
nginx.conf.sample
. Copy and rename it to make your changes. -
Open the file located in
<magento2_installation_directory>/nginx.conf.sample
.
-
-
Modify the Hypertext PHP Entry Point:
Find this section:
`# PHP entry point for the main application
location ~ (index|get|static|report|404|503|health_check)\.php$ {
try_files $uri =404;
fastcgi_pass fastcgi_backend;
fastcgi_buffers 1024 4k;
fastcgi_param PHP_FLAG "session.auto_start=off \n suhosin.session.cryptua=off";
fastcgi_param PHP_VALUE "memory_limit=1G \n max_execution_time=18000";
fastcgi_read_timeout 600s; fastcgi_connect_timeout 600s;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $ document_root$fastcgi_script_name;
include fastcgi_params;
}`.
-
Add Multisite Customization:
- Before the
include
line, add these two lines:
- Before the
`fastcgi_param MAGE_RUN_TYPE $MAGE_RUN_TYPE;
fastcgi_param MAGE_RUN_CODE $MAGE_RUN_CODE;`.
- The updated section should now look like this:
`# PHP entry point for the main application
location ~ (index|get|static|report|404|503|health_check)\.php$ {
try_files $uri =404;
fastcgi_pass fastcgi_backend;
fastcgi_buffers 1024 4k;
fastcgi_param PHP_FLAG "session.auto_start=off \n suhosin.session.cryptua=off";
fastcgi_param PHP_VALUE "memory_limit=1G \n max_execution_time=18000";
fastcgi_read_timeout 600s; fastcgi_connect_timeout 600s;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $ document_root$fastcgi_script_name;
# START - Multisite customization
fastcgi_param MAGE_RUN_TYPE $MAGE_RUN_TYPE;
fastcgi_param MAGE_RUN_CODE $MAGE_RUN_CODE;
# END - Multisite customization
include fastcgi_params;
}`.
Step 4: Update Root URL Configuration
-
Update the Base URL for the French and German Websites:
-
Log in to the Magento store admin and go to Stores > Settings > Configuration > General > Web.
-
Change the configuration scope to the French website.
-
Update the Base URL and Base Link URL to:
-
http://french.magento24.com
https://french.magento24.com
for secure URLs.
http://german.magento24.com
https://german.magento24.com
for secure URLs.
-
Clean Cache:
Run this command to clean the configuration and full-page caches:
bin/magento cache:clean config full_page.
You can choose MGT as your hosting provider and let its NGINX-optimized server handle cache management for you. It delivers optimal performance without needing any manual effort and makes your store run smoothly.
Optimizing Performance for Magento2 NGINX Multi Website
1. Enable Caching
-
Use Built-in Caching: Turn on full-page caching to store copies and static content. It reduces server load and speeds up response times for each mage_run_type website.
-
Varnish Cache: Set up Varnish Cache to improve response times by caching content.
-
Redis: Use Redis for backend caching. It stores data in memory, reducing database queries and boosting performance.
2. Optimize Images and Media
-
Compress Images: Compress images to reduce file sizes. It helps pages load faster on your website.
-
Use a CDN: Add a Content Delivery Network (CDN) to serve static content from servers closer to users. This reduces latency and speeds up load times.
3. Setup Server Software Efficiently
-
Use Efficient Server Blocks: Set up NGINX server blocks with listen 80; server_name directives. Use settings like
root,
location,
and http_host to manage several websites. -
Enable GZIP Compression: Turn on GZIP compression to shrink file sizes. It improves load times for the websites.
-
Leverage HTTP/2: Use server software’s HTTP/2 support. It improves performance with features like request multiplexing and server push. This enhances the entry point for main application.
4. Magento Configuration
-
Production Mode: Set the ecommerce store to mage_mode production. It disables unnecessary features, improving performance.
-
Flat Catalogs: Enable flat catalogs for products and categories. It speeds up data retrieval by reducing query times.
-
Update Indexers: Regularly update indexers to ensure data displays quickly on the front end.
5. Scalability and Load Balancing
-
Use Load Balancing: Set up the server as a load balancer to distribute traffic across servers. It improves the reliability of each website.
-
Scalable Architecture: Design a scalable system. The setup allows you to handle more traffic and data without big server changes.
6. Security Considerations
-
SSL/TLS Termination: Use server software for SSL/TLS termination. It secures communication between clients and servers, protecting sensitive URI data.
-
Rate Limiting and Access Control: Set up rate limiting and access control in the server. It prevents harmful traffic and protects your mage_root.
Magento 2 NGINX Multi Website: Security and SSL Configuration Tips
1. SSL Certificate Installation
-
Obtain an SSL Certificate: Buy an SSL certificate from a trusted Certificate Authority. Each website needs its certificate.
-
Place Certificate Files: Store the SSL certificate, private key, and intermediate chain files in a secure folder. You can also refer to GitHub for guides on managing SSL certificates.
-
Update Server Configuration: Edit your virtual host setup files. It helps to add the
ssl_certificate
andssl_certificate_key
settings. Make sure thelisten
directive is set to443 ssl
. Here’s an example:
`server {
listen 443 SSL;
ssl_certificate /etc/ssl/certs/yourdomain.crt;
ssl_certificate_key /etc/ssl/certs/yourdomain.key;
ssl_protocols TLSv1.2 TLSv1.3;
}`.
Note: For HTTP traffic, use listen 80; server_name to redirect users to HTTPS.
- Restart NGINX: Apply the changes by restarting the server software:
sudo systemctl restart nginx.
2. Secure Configuration
-
Limit SSL Protocols: Use only modern TLS protocols (TLSv1.2 and TLSv1.3). This protects your site from vulnerabilities in older protocols.
-
Use Strong Ciphers: Setup server to use strong ciphers:
`ssl_ciphers EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH;
ssl_prefer_server_ciphers on;`.
This ensures your data is encrypted securely.
- Enable HTTP/2 Support: Enable HTTP/2 in software to improve both performance and security.
3. Additional Security Measures
-
Rate Limiting: Set up rate limiting in the HTTP server. It protects your site from DDoS attacks and brute-force login attempts by controlling traffic.
-
Access Control: Restrict access to critical areas. Use IP whitelisting or access rules to block unauthorized users.
-
Content Filtering: Use content filtering to block harmful files from being served.
-
IP Whitelisting: Trusted IP addresses allow only approved users to access sensitive areas.
4. Regular Maintenance
-
Update Software: Keep hypertext updated with the latest versions. It ensures security patches are applied.
-
Monitor Logs: Regularly check server logs for unusual activity. It can help you detect Magento security issues early.
-
Backup Data: Back up your site’s data regularly. It ensures you do not lose important information during a security breach. This is a key additional task in keeping your site safe.
Common Issues When Configuring NGINX Multi Website
Common Issue | Details |
---|---|
Incorrect NGINX Setup | Errors in server host configuration files can prevent new websites from loading. Make sure each server block is set up correctly for each domain. |
Cache and Indexing Problems | Cache issues can show incorrect content. Clear the Magento cache and NGINX cache after changes. Update indexers regularly when setting up multiple sites. |
Improper Use of Directives | Incorrect use of listen 80; server_name or root can cause routing problems. Verify paths match the online store setup. |
Conflicts with Extensions | Some ecommerce extensions may not work well with a multi-website setup. Test them in a staging ground badges environment before using them on live sites. |
Base URL Misconfiguration | Each website needs a unique root URL store panel. Incorrect root URLs can cause redirect loops or loading issues. |
Improper Setup Redirects | Incorrect redirects can cause loops or loading issues. Set up the redirects correctly for each domain using listen 80; server_name . |
SSL Certificate Issues | Incorrect SSL certificate setup can show security warnings. Ensure each website has its own SSL certificate and uses TLSv1.2 or higher. |
Performance Bottlenecks | Not using fastcgi_read_timeout 600s; fastcgi_connect_timeout 600s; fastcgi_index can cause slow performance. Enable caching to improve site speed. |
MAGE_RUN_TYPE and MAGE_RUN_CODE Errors | Incorrect mage_run_type; fastcgi_param mage_run_code can load the store. Set these values correctly. |
DNS Issues | Incorrect DNS settings can stop domains from loading. Make sure DNS points to the correct server IP and verify each site is working properly. |
File Permissions | Wrong permissions in the root directory can lead to "403 Forbidden" errors. Set directories to 755 and files to 644 to avoid issues. |
FAQs
1. How do you set up Magento 2 NGINX for multiple websites?
To set up NGINX for several websites, set up an nginx setup. Use listen 80; server_name
to define server details. Set the correct mage_run_type; fastcgi_param mage_run_code to load the right store. Add the store code for each sublevel domain in the configuration.
2. How can I use one virtual server host to manage several websites?
You can use one virtual server host to manage several websites by adding server blocks. It allows the server to accept multiple domains in one instance. You can also customize each store while keeping the configuration simple.
mage_mode developer; set
?
3. How do I set up NGINX for Magento in For staging ground badges, use mage_mode developer; set in the nginx setup. This helps you see errors and troubleshoot faster. It is good to use a custom configuration to keep development and production separate.
mage_run_type store; include
in a multi-website setup?
4. What is the role of mage_run_type store; include specifies that can load a store view or the default store. The setting helps online sites know which store to load. It is easier to manage new sites in a multi-website setup.
5. How can I add a subdomain for a new Magento website using NGINX?
To add a sublevel domain for a new website, edit your HTTP server setup. Add a server block with the correct settings for server_name
. Include the store code to ensure the loads are in the proper online store.
6. How can I clone this repository to set up multiple websites?
You can clone this repository from GitHub to get the configuration files for your setup. These files show you how to configure the server to help you get started with the nginx setup.
Summary
Magento 2 NGINX Multi Website Setup is a configuration that lets you manage multiple websites. It uses NGINX as the web server. The setup allows you to create separate websites, each with its domain or subdomain. Following the considerations:
-
Canonicalization: Use canonical tags to stop duplicate content.
-
Base URL Configuration: Set the correct base URLs for each domain.
-
SSL Configuration: Secure each website with an SSL certificate.
-
Sitemap Management: Create XML sitemaps and submit them to search engines.
-
Cross-Origin Resource Sharing: Set up CORS to prevent cross-origin errors.
Explore Magento hosting services to manage multiple websites with NGINX.