LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 04-12-2021, 12:48 PM   #1
gusto1
Member
 
Registered: Dec 2019
Posts: 35

Rep: Reputation: Disabled
redirect non www to www nginx reverse proxy


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 help
 
Old 04-12-2021, 11:15 PM   #2
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,802

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Quote:
Originally Posted by gusto1 View Post
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;
    }
}
I've not seen this syntax in the nginx config file.

What happens when you repeat your test after changing the server_name record to:
Code:
    server_name www.domain-name1.com domain-name1.com;
 
Old 04-13-2021, 09:24 AM   #3
gusto1
Member
 
Registered: Dec 2019
Posts: 35

Original Poster
Rep: Reputation: Disabled
you can see the configuration everywhere
Code:
 server_name example.org www.example.org;
http://nginx.org/en/docs/http/request_processing.html
first non www and then www
 
Old 04-17-2021, 03:19 PM   #4
jdrosales
LQ Newbie
 
Registered: Feb 2020
Location: Virginia, USA
Distribution: Ubuntu, Debian
Posts: 21
Blog Entries: 1

Rep: Reputation: 3
There are a few questions/things you should check/try.

- How are named de domains in the containers? With or without the www.?
- In your proxy_set_header you are instructing nginx to pass the FIRST name on your config regardless of the redirection.
- Try changing $host in your proxy_set_header to $http_host, which should contain the value of the Header sent by the browser request.

And this is just out of curiosity, why do you need the leading www.? I actually strip that part to all my domains.

Anyway, try / check those suggestions and let us know. It should not be too difficult to get it going as you need it.
 
Old 04-18-2021, 05:13 AM   #5
gusto1
Member
 
Registered: Dec 2019
Posts: 35

Original Poster
Rep: Reputation: Disabled
I found the manul on nginx web. It work perfectly now.
Code:
server {
    listen 80;
    server_name domain-name1.com;
    return 301 http://www.domain-name1.com$request_uri;
}

server {
    listen 80;
    server_name 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;
    return 301 http://www.domain-name2.com$request_uri;
}

server {
    listen 80;
    server_name 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;
    }
}
Is it The configuration correctly ?
 
  


Reply



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
Nextcloud login loop on Nginx behind Nginx reverse proxy horizn Linux - Server 0 12-27-2018 02:44 PM
redirect http to https on nginx as reverse proxy vincix Linux - Server 1 12-09-2017 05:40 PM
Nginx, Horde, Https, Redirect, This webpage has a redirect loop axetone Ubuntu 0 02-08-2015 10:44 PM
Reverse Proxy for non-web (non-http) ? cheezy Linux - Server 3 07-23-2014 09:54 PM
Nginx Reverse proxy on a internal apache reverse server ITiger Linux - Software 0 04-25-2014 07:44 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 03:17 PM.

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