Need to stop Magento running on nginx from redirecting from http to https on front end.

Need to stop Magento running on nginx from redirecting from http to https on front end.

September 24, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Need to stop Magento running on nginx from redirecting from http to https on front end.

Nginx is a popular web server known for its high performance, stability, and flexibility. It’s often used as a reverse proxy server for high-traffic websites due to its ability to handle large numbers of simultaneous connections efficiently. In this post, we’ll explore some best practices for configuring Nginx for optimal performance on a Laravel application.

Basic Nginx Configuration

The default configuration file /etc/nginx/nginx.conf serves as the foundation for all your server configurations. Here are some key directives you should pay attention to:

  • User: This specifies the user that Nginx will run as. For security purposes, it’s recommended to use a non-root user.

  • Worker Processes: This directive determines how many worker processes Nginx will spawn. The number can be set to auto for automatic detection or you can specify an integer value.

  • Worker Connections: This dictates the maximum number of simultaneous connections that each worker process can handle.

  • Open File Cache: This is a critical setting for performance optimization. It allows Nginx to cache frequently accessed files, reducing the overhead of opening and closing them.

open_file_cache max=10000 inactive=3600s;
open_file_cache_valid 7200s;
open_file_cache_min_uses 2;

This configuration sets a maximum cache size of 10,000 files that are inactive after 3600 seconds (1 hour). The valid directive specifies the time before an entry is considered stale and should be re-checked with the backend application.

Gzip Compression

Gzipping your content can significantly reduce page weight and improve load times. Nginx supports gzip compression out of the box:

gzip                on;
gzip_vary           on;
gzip_proxied        any;
gzip_types          application/javascript application/x-javascript text/javascript text/css;
gzip_buffers        16 8k;
gzip_comp_level     6;

This configuration enables gzip compression and specifies the types of content that should be compressed. The gzip_comp_level directive determines the level of compression, with higher values resulting in more efficient compression.

Server Blocks

Each domain or subdomain you want to serve should have its own server block in /etc/nginx/sites-enabled/. A typical Magento configuration might look like this:

server {
    listen 80;
    listen 443 http2 ssl;
    #... SSL configuration ...

    server_name www.overnightsupplements.com;

    root /var/www/html/overnight;
    index index.php index.html index.htm;

    location = /js/index.php/x.js {
       rewrite ^(.*\.php)/ $1 last;
       }

    try_files $uri $uri/ @rewrite;
}

server {
    listen 80;
    return 301 $scheme://www.WEBSITENAME.com$request_uri;
}

The first block handles the main domain and its SSL configuration. The try_files directive is a key feature of Nginx that allows you to specify a list of files or directories to check in order.

The second block redirects non-www requests to their www equivalent. This is important for maintaining consistency in your domain structure.

PHP Configuration

For PHP processing, you’ll need to configure the fastcgi_pass directive to point to your PHP-FPM service:

location ~ \.php$ {
    include /etc/nginx/conf.d/headers.conf;
    try_files $uri =404;
    fastcgi_pass 127.0.0.1:9000;
    #... other directives ...
}

The fastcgi_param SCRIPT_FILENAME directive is crucial as it specifies the path to the PHP script being executed.

Security Headers

Implementing security headers can help protect your application from common web attacks:

add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";
add_header X-Frame-Options DENY;
add_header X-Content-Type-Options nosniff;

The Strict-Transport-Security header enforces secure communication over HTTPS. The X-Frame-Options and X-Content-Type-Options headers prevent your site from being embedded in other sites (clickjacking) and ensure that content is interpreted correctly.

Conclusion

Optimizing Nginx configuration for a Laravel application involves a combination of performance tuning, security measures, and efficient handling of requests. By following the best practices outlined above—such as enabling gzip compression, configuring open file cache, setting appropriate worker processes, and implementing security headers—you can ensure that your application performs optimally under high loads.

Remember to always test your changes thoroughly in a staging environment before deploying them to production. This will help you identify any potential issues or conflicts with existing configurations.

At Laravel Company, we specialize in Laravel development and can provide expert guidance on optimizing your application’s performance, security, and scalability. Contact us today to learn more about how we can help you achieve your business goals through custom software solutions.

Cache Performance Security Configuration

Our network