Search code examples
wordpressnginxsubdomain

How can I set sub domain to show static page in my website?


I added name server in Vultr.

enter image description here

and I want it to load a page when I use that name server

edu.mrtrobotics.com -----> mrtrootics.com/page

I am using wordpresss and nginx. what should I do?? do I have to change something in nginx to redirect to a specific page when user used subdomain? or it is declared in DNS??? I ve got no idea at all..

server {
    listen 80;
    listen [::]:80;

    server_name mrtrobotics.com www.mrtrobotics.com;

    location ~ /.well-known/acme-challenge {
        allow all;
        root /var/www/html;
    }

    location / {
        rewrite ^ https://$host$request_uri? permanent;
    }
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name mrtrobotics.com www.mrtrobotics.com;

    index index.php index.html index.htm;

    root /var/www/html;

    server_tokens off;

    ssl_certificate /etc/letsencrypt/live/mrtrobotics.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/mrtrobotics.com/privkey.pem;

    include /etc/nginx/conf.d/options-ssl-nginx.conf;

    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-XSS-Protection "1; mode=block" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header Referrer-Policy "no-referrer-when-downgrade" always;
    add_header Content-Security-Policy "default-src * data: 'unsafe-eval' 'unsafe-inline'" always;
    # add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
    # enable strict transport security only if you understand the implications

    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass wordpress:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }

    location ~ /\.ht {
        deny all;
    }

    location = /favicon.ico {
        log_not_found off; access_log off;
    }
    location = /robots.txt {
        log_not_found off; access_log off; allow all;
    }
    location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
        expires max;
        log_not_found off;
    }
}

# Set client upload size - 100Mbyte
client_max_body_size 100M;

# to avoid 504 time out error - defalut is 60s
proxy_send_timeout 180s;
proxy_read_timeout 180s;
fastcgi_send_timeout 180s;
fastcgi_read_timeout 180s;

Solution

  • That's in nginx config. You'll need to add the following (assuming your certificate works for edu. as well as your primary domain):

    server {
        listen 80;
        listen 443 ssl http2;
        listen [::]:443 ssl http2;
        server_name edu.mrtrobotics.com;
    
        ssl_certificate /etc/letsencrypt/live/mrtrobotics.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/mrtrobotics.com/privkey.pem;
    
        include /etc/nginx/conf.d/options-ssl-nginx.conf;
    
        rewrite ^ https://mtrobotics.com/some-page permanent;
    }
    

    This should redirect any request to edu.mtrobotics.com (with any path or parameters) to https://mtrobotics.com/som-page.