tengine Remove $host fixes

vvppvyoh  于 2022-12-31  发布在  其他
关注(0)|答案(4)|浏览(134)

Hello all.

I have a problem. You know - browsers are not so clever. If you write a link with . at the end (ex github.com.) you'll have problems. It can be...anything. Not working cookie, missed link, e.t.c. Nginx (and Tengine) corrects $host and remove bugs from it. So when i try to use return 301 or rewrite it don't works. Because configs don't see '.' at the end.

My please is to do a config value:

correct_uri_path on/off;

Thanks in advance,

Alex.

mctunoxg

mctunoxg1#

Do you wan to keep the last dot in the host?

I just do a experiment. It works for me:

GET / HTTP/1.1
User-Agent: Wget/1.13.1 (linux-gnu)
Accept: */*
Host: 127.0.0.1:1980
Connection: Keep-Alive

---request end---
HTTP request sent, awaiting response...
---response begin---
HTTP/1.1 301 Moved Permanently
Server: Tengine/1.4.4
Date: Sat, 23 Mar 2013 04:33:58 GMT
Content-Type: text/html
Content-Length: 286
Connection: keep-alive
Location: http://yaoweibin.cn./

My config is like this:

location / {
         root   html;
         index  index.html index.htm;
         return 301 http://yaoweibin.cn./;
   }

I'm curious why do you want such behaviour?

yc0p9oo0

yc0p9oo02#

You did not understand.

If user adds (probably by mistake) dot at the end - there will be problem's on the website. It can be any. Cookie mismatch, search engines aggresive, 404. Anyway user will leave your website.

I want to use

server {

...    

# Remove www.
if ( $host ~ ^(www\.)?(.+)$ ) {
    set $sathost $2;
}

# Redirect domain with dot at the end to domain without dot [don't works because nginx fixes $host]
if ($sathost ~* ^[a-z0-9-_]+\.[a-z0-9-_]+\.$) {
    return 301 $2;
}

# set variable for future use
if ($sathost ~* ^([a-z0-9-_]+\.[a-z0-9-_]+)$) {
    set $domain "/$1";
}

# set variables for future use
if ($sathost ~* ^([a-z0-9-_\.]+)\.([a-z0-9-_]+\.[a-z0-9-_]+)$) {
    set $subdomain1 "/$1";
    set $domain "/$2";
}

...

}

zyfwsgd6

zyfwsgd63#

Can you use the variable $http_host instead? It could be exactly the same host name in the http header. You can rediect the wrong host request like this:

location / {
        if ($http_host ~ "\.(:\d+)?$") {
            return 301 http://$host/;
        }
    }
aij0ehis

aij0ehis4#

Can you use the variable $http_host instead? It could be exactly the same host name in the http header. You can rediect the wrong host request like this:

location / {
    if ($http_host ~ "\.(:\d+)?$") {
        return 301 http://$host/;
    }
}

It works like a charm, thanks.

How do you think - it is fine for most workloads to redirect all GET and HEAD requests with dot at the end to request without dot? For example "domain.com/image.jpg." redirect to "domain.com/image.jpg"?

If yes, can you give most correct (from nginx prespective) and performant code to realize that?

相关问题