skip to Main Content

I want to convert apache VHost to Nginx vhost.
this is my Apache Vhost and working fine.

<VirtualHost *:80>
  ServerName    w2.valleymedtrans.com
  ServerAdmin   "[email protected]"

  DocumentRoot /var/www/html/website/valleymedtrans.com/website2019
<Directory /var/www/html/website/valleymedtrans.com/website2019>
       AllowOverride All
        Require all granted
        DirectoryIndex index.php
</Directory>
  CustomLog /var/log/httpd/access.log combined
  ErrorLog /var/log/httpd/error.log
RewriteEngine on
RewriteCond %{SERVER_NAME} =w2.valleymedtrans.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

I want to convert into Nginx but getting error w2.valleymedtrans.com redirected you too many times.
my Nginx Vhost

server {
        listen 80;
        listen [::]:80;
        server_name w2.valleymedtrans.com www.w2.valleymedtrans.com;
        root /var/www/html/valleymedtrans.com/website2019;
        index index.html;
        return 301 https://w2.valleymedtrans.com$request_uri;
        error_log  /var/log/nginx/error_log warn;
        access_log  /var/log/nginx/access.log combined ;
        
        location / {
                
                try_files $uri $uri/ /index.php?$args;

        }

        location ~ .php$ {
                include snippets/fastcgi-php.conf;
        
                fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
                include fastcgi_params;
        }
      
    listen [::]:443 ssl; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/w2.valleymedtrans.com-0001/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/w2.valleymedtrans.com-0001/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}

please help me how I can fix the redirecting issue.

2

Answers


  1. Try

    server {
        listen 80;
        listen [::]:80;
        server_name w2.valleymedtrans.com;
        return 301 https://w2.valleymedtrans.com$request_uri;
    }
    
    server {
        listen [::]:443 ssl; # managed by Certbot
        listen 443 ssl; # managed by Certbot
        ssl_certificate /etc/letsencrypt/live/w2.valleymedtrans.com-0001/fullchain.pem; # managed by Certbot
        ssl_certificate_key /etc/letsencrypt/live/w2.valleymedtrans.com-0001/privkey.pem; # managed by Certbot
        include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
        ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
    
        server_name w2.valleymedtrans.com;
        root /var/www/html/valleymedtrans.com/website2019;
        index index.php index.html;
        error_log  /var/log/nginx/error_log warn;
        access_log  /var/log/nginx/access.log combined ;
            
        location / {
            try_files $uri $uri/ /index.php$is_args$args;
        }
    
        location ~ .php$ {
            include fastcgi_params;
            include snippets/fastcgi-php.conf;
    
            fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        }
    }
    
    Login or Signup to reply.
  2. Your config-file for nginx looks good. But I dont know if you can handle multiple listeners in one config-file.

    It is better to have a config file for listening on Port 80 and one config file for listening on port 443. As you have no physical seperation in this file, the line return 301 https://w2.valleymedtrans.com$request_uri; could also be interpreted by the listener of Port 443, which could cause a redirect-loop.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search