V
V
Vitali2013-09-25 11:32:55
Nginx
Vitali, 2013-09-25 11:32:55

Forced removal of www from a link (Nginx, HTTPS, Passenger)?

I googled half of the Internet and unfortunately did not find a working solution.

The server runs a Rails application under Passenger (which runs on Nginx). In addition to all this, the site has a forced redirect to the https version of the site.

Redirecting to the https version of the site is as follows:

server {<br>
        listen 80;<br>
        rewrite ^(.*) https://example.ru$1 permanent;<br>
    }<br>


Below there is already a server block that listens on port 443 from server_name example.ru

server {<br>
        listen   443;<br>
        server_name  example.ru;<br>
        #rewrite     ^   https://$server_name$request_uri? permanent;<br>
<br>
        ssl on;<br>
        ssl_certificate /opt/nginx/conf/certs/example.ru.crt;<br>
        ssl_certificate_key /opt/nginx/conf/certs/example.ru.key.nopass;<br>
<br>
        charset utf-8;<br>
        root /var/www/public;<br>
        passenger_enabled on;<br>
        rails_env production;<br>
<br>
        error_page   500 502 503 504  /50x.html;<br>
        location = /50x.html {<br>
            root   html;<br>
        }<br>
    }<br>


How to force redirect all requests to example.ru (https) even if www.example.ru (http) was requested

I really count on your help.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
rakeev, 2013-09-25
@Screatch

And server_name in that block is not specified at all? It should be:

server {
    listen 80;
    server_name example.com www.example.com;
    rewrite ^(.*) https://example.com$1 permanent;
}

M
Maximus43, 2013-09-25
@Maximus43

server {
        listen  80;
        server_name  www.example.com;

location ~ (?<URL>^.*) {
                return 301 https://example.com$url$is_args$args;
                }
}

Y
yumitsu, 2013-09-25
@yumitsu

And why exactly rewrite, and not this line in config/environments/production.rb:

config.force_ssl = true

True, this will not redirect from www. to the main domain.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question