Article 5GG38 redirect non www to www nginx reverse proxy

redirect non www to www nginx reverse proxy

by
gusto1
from LinuxQuestions.org on (#5GG38)
I use the nginx reverse proxy in front of the apache web servers.
The nginx configuration file

Code:server {
listen 80;
server_name domain-name1.com www.domain-name1.com;

location / {
proxy_pass http://192.168.1.110;
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;
}
}

server {
listen 80;
server_name domain-name2.com www.domain-name2.com;

location / {
proxy_pass http://192.168.1.111;
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;
}
}When I open a web browser and type domain-name1.com (domain-name2.com) it works great.

When I open a web browser and type www.domain-name1.com (www.domain-name2.com) I see then "The webpage is not available".
I would like domain-name1.com to be redirected to www.domain-name1.com.
I edited the configuration file

Code:server {
listen 80;
server_name domain-name1.com;
return 301 $scheme://www.domain-name1.com$request_uri;

location / {
proxy_pass http://192.168.1.110;
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;
}
}When I open a web browser and type domain-name1.com , it is redirected to www.domain-name1.com but I still see "The webpage is not available".
The domain name and cname for www are valid on dns server.
Thank you for everyone helplatest?d=yIl2AUoC8zA latest?i=HkHfixlE5v4:WEpbI8JBCao:F7zBnMy latest?i=HkHfixlE5v4:WEpbI8JBCao:V_sGLiP latest?d=qj6IDK7rITs latest?i=HkHfixlE5v4:WEpbI8JBCao:gIN9vFwHkHfixlE5v4
External Content
Source RSS or Atom Feed
Feed Location https://feeds.feedburner.com/linuxquestions/latest
Feed Title LinuxQuestions.org
Feed Link https://www.linuxquestions.org/questions/
Reply 0 comments