0

I have done the redirection on NGINX but it still not working.

first, I need to redirect http://www.example.com to https://www2.example.com

also, redirect https://www.example.com to https://www2.example.com

I have the following conf:


[root@andysrv5-nginx ~]# cat /etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;
    error_log   /var/log/nginx/error.log debug;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;


######   setting for crm.example.com http  
     server {
        listen 80;
        server_name crm.example.com;
        access_log off;
        location / {
              proxy_pass http://172.31.16.207/;
              proxy_redirect off;
              proxy_set_header Host $host;
              proxy_set_header X-Real-IP $remote_addr;
              proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
              proxy_max_temp_file_size 0;
              client_max_body_size 10m;
              client_body_buffer_size 128k;
              proxy_connect_timeout 90;
              proxy_send_timeout 90;
              proxy_read_timeout 90;
              proxy_buffer_size 4k;
              proxy_buffers 4 32k;
              proxy_busy_buffers_size 64k;
              proxy_temp_file_write_size 64k;
                }
     }

######   setting for crm.example.com https  


    server {
        listen 443 ssl http2;
        server_name crm.example.com;
        server_tokens off;
        access_log off;
        ssl_certificate "/etc/pki/nginx/fullchain-1657107464.pem";
        ssl_certificate_key "/etc/pki/nginx/privkey-1657107464.pem";
        ssl_session_cache shared:SSL:1m;
        ssl_session_timeout  10m;
        ssl_ciphers HIGH:!aNULL:!MD5:!EXPORT:!DES;
        ssl_prefer_server_ciphers on;
        location / {
                proxy_pass https://172.31.16.207/;
                proxy_redirect off;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto $scheme;
                }
          if ($request_method !~ ^(GET|HEAD|POST)$ )
            {
            return 405;
            }
        }


######   setting for www2.example.com http  

    server {
        listen 80;
        server_name www.example.com;
        return 301 https://www2.example.com$request_uri;
        }


    server {
        listen 80;
        server_name www2.example.com;
        return 301 https://www2.example.com$request_uri;
        }




######   setting for www2.example.com https  ####################


    server {
        listen 443 ssl http2;
        server_name www.example.com;
        return 301 https://www2.example.com$request_uri;
        }


    server {
        listen 443 ssl http2;
        server_name www2.example.com;
        server_tokens off;
        ssl_certificate "/etc/pki/nginx/www2.certificate.crt";
        ssl_certificate_key "/etc/pki/nginx/www2.private.key";
        ssl_session_cache shared:SSL:1m;
        ssl_session_timeout  10m;
        ssl_ciphers HIGH:!aNULL:!MD5:!EXPORT:!DES;
        ssl_prefer_server_ciphers on;

        location / {
                proxy_pass https://10.10.10.209/;
                proxy_redirect off;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto $scheme;
                }
          if ($request_method !~ ^(GET|HEAD|POST)$ )
            {
            return 405;
            }
        }

}

Could you please help with what I did wrong?!!

25
  • Is https://www2.example.com loading correctly?
    – Paul
    Commented Aug 24, 2022 at 13:08
  • yes, loaded correctly Commented Aug 24, 2022 at 13:12
  • Is there anything in the logs when requesting http://www.example.com?
    – Paul
    Commented Aug 24, 2022 at 13:17
  • I disabled access log and error log because nginx not started with them on Commented Aug 24, 2022 at 13:26
  • 1
    What is the redirect that is not working? Please show the exact request you make and the response for it. Commented Aug 24, 2022 at 20:30

0

You must log in to answer this question.

Browse other questions tagged .