LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 04-03-2023, 07:19 AM   #1
tiashi
LQ Newbie
 
Registered: Nov 2022
Location: Moon
Posts: 25

Rep: Reputation: 0
nginx redirect issue from www to non-www


Hi, friends... it's a nginx question, I've been stuck on this issue for quite a while and I was unable to find a solution. I'm posting here hope that some brilliant minds in this community can help.

My issue is that I have a domain name tianyushi.net and now it works well. I want to redirect www.tianyushi.net to tianyushi.net.

I've used a CNAME for DNS, and when I dig the www domain, I get the following results:

Code:
tiashi@LSGC02DT7TFMD6R notebook % dig www.tianyushi.net

;; ANSWER SECTION:
www.tianyushi.net.	680	IN	CNAME	tianyushi.net.
tianyushi.net.		681	IN	A	162.255.119.225
So I believe that the DNS side is good.

However, when I try to open www.tianyushi.net, I get a 404 error, which I think is a problem on the Nginx side.

I used the this config with instructions from Google:

Code:
server {
    listen 80;
    listen [::]:80;
    server_name tianyushi.net;
    root /var/www/html;
    index index.html index.nginx-debian.html;

    location / {
        try_files $uri $uri/ =404;
    }
}

server {
    listen 80;
    listen [::]:80;
    server_name www.tianyushi.net;
    return 301 $scheme://tianyushi.net$request_uri;
}
I have tried other different configs that I found on Google, but I still can't get it to work. pls help...
 
Old 04-03-2023, 07:43 AM   #2
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,163
Blog Entries: 1

Rep: Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032
Quote:
server {
listen 80;
listen [::]:80;
server_name tianyushi.net;
root /var/www/html;
index index.html index.nginx-debian.html;

location / {
try_files $uri $uri/ =404;
}
}

server {
listen 80;
listen [::]:80;
server_name www.tianyushi.net;
return 301 $scheme://tianyushi.net$request_uri;
}

I have tried other different configs that I found on Google, but I still can't get it to work. pls help...
It should work. Just make sure you restart nginx after editing config files and clear your browser cache when testing

You could also use a single server block using both hostnames in server_name:
Code:
server {
    listen 80;
    listen [::]:80;
    server_name tianyushi.net www.tianyushi.net;
    root /var/www/html;
    index index.html index.nginx-debian.html;

    location / {
        try_files $uri $uri/ =404;
    }
}
 
Old 04-03-2023, 12:08 PM   #3
tiashi
LQ Newbie
 
Registered: Nov 2022
Location: Moon
Posts: 25

Original Poster
Rep: Reputation: 0
Unhappy

Quote:
Originally Posted by bathory View Post
It should work. Just make sure you restart nginx after editing config files and clear your browser cache when testing

You could also use a single server block using both hostnames in server_name:
Code:
server {
    listen 80;
    listen [::]:80;
    server_name tianyushi.net www.tianyushi.net;
    root /var/www/html;
    index index.html index.nginx-debian.html;

    location / {
        try_files $uri $uri/ =404;
    }
}
hi thanks bathory, I reloaded & restarted nginx, still not working... may I ask how can I debug this issue? I even cannot see any access or error logs for nginx when accessing www.tianyushi.net, only can get this 404 err info from the browser..

Summary
URL: http://www.tianyushi.net/
Status: 404 Not Found
Source: Network
Address: 162.255.119.225:80

Request
GET / HTTP/1.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Upgrade-Insecure-Requests: 1
Host: www.tianyushi.net
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.6.1 Safari/605.1.15
Accept-Language: en-us
Accept-Encoding: gzip, deflate
Connection: keep-alive

Response
HTTP/1.1 404 Not Found
Date: Mon, 03 Apr 2023 17:05:27 GMT
Content-Length: 0
Connection: keep-alive
Server: namecheap-nginx
X-Served-By: Namecheap URL Forward
 
Old 04-03-2023, 12:28 PM   #4
tiashi
LQ Newbie
 
Registered: Nov 2022
Location: Moon
Posts: 25

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by bathory View Post
It should work. Just make sure you restart nginx after editing config files and clear your browser cache when testing

You could also use a single server block using both hostnames in server_name:
Code:
server {
    listen 80;
    listen [::]:80;
    server_name tianyushi.net www.tianyushi.net;
    root /var/www/html;
    index index.html index.nginx-debian.html;

    location / {
        try_files $uri $uri/ =404;
    }
}
hi bathory... I changed my conf with your help, but still with the same issue... I cannot get any log from nginx side, do you think it's a DNS issue?
 
Old 04-03-2023, 04:15 PM   #5
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,163
Blog Entries: 1

Rep: Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032
Quote:
Originally Posted by tiashi View Post
hi bathory... I changed my conf with your help, but still with the same issue... I cannot get any log from nginx side, do you think it's a DNS issue?
It's not a DNS issue. Both hostnames resolve correctly to the same IP

Perhaps your nginx server is not aware of www.tianyushi.net. Check the config file(s) for the various server_name directives.
If you're running a debian based distro look under /etc/nginx/sites-enabled for the vhosts that nginx is aware of.
 
Old 04-03-2023, 10:23 PM   #6
tiashi
LQ Newbie
 
Registered: Nov 2022
Location: Moon
Posts: 25

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by bathory View Post
It's not a DNS issue. Both hostnames resolve correctly to the same IP

Perhaps your nginx server is not aware of www.tianyushi.net. Check the config file(s) for the various server_name directives.
If you're running a debian based distro look under /etc/nginx/sites-enabled for the vhosts that nginx is aware of.
Hi bathory, after getting several 404 errs, I revisited namecheap's website and added a redirect for the 'www' subdomain to point to 'tianyushi.net' with a Permanent (301) on namecheap side. This solved the issue, but I'm curious if you think it was a configuration issue with Namecheap's side nginx. When I checked the DNS records using the 'dig' command, it returned an IP address of '162.255.119.225', which is namecheap's nginx server instead of my own. It appears that namecheap was forwarding the traffic instead of directing it to my server.

However, I'm still unsure why I was unable to bypass namecheap's settings by redirecting the 'www' subdomain to the non-www version on my own nginx server.
 
Old 04-04-2023, 01:06 AM   #7
tiashi
LQ Newbie
 
Registered: Nov 2022
Location: Moon
Posts: 25

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by bathory View Post
It's not a DNS issue. Both hostnames resolve correctly to the same IP

Perhaps your nginx server is not aware of www.tianyushi.net. Check the config file(s) for the various server_name directives.
If you're running a debian based distro look under /etc/nginx/sites-enabled for the vhosts that nginx is aware of.
I got the root cause, thanks again for the advice! Initially, I had been using namecheap DNS to directly redirect the domain to my host: port, which was using its own nginx server. However, I have now updated the DNS settings by mapping the domain to my host IP using an A record, and the nginx server on my side is now working properly. Thank you for your prompt response!

Last edited by tiashi; 04-04-2023 at 08:23 AM.
 
  


Reply

Tags
nginx, www or no www



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
redirect non www to www nginx reverse proxy gusto1 Linux - Server 4 04-18-2021 05:13 AM
Trailing slash issue with https to http redirect (Nginx/BitrixVM/CentOS6.5) Akram03 Linux - General 0 02-21-2016 04:10 PM
[SOLVED] Trailing slash issue with https redirect - Nginx linson_85 Linux - General 2 09-23-2015 11:43 PM
Nginx, Horde, Https, Redirect, This webpage has a redirect loop axetone Ubuntu 0 02-08-2015 10:44 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 03:53 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration