Z
Z
zelsky2015-09-10 10:07:11
Nginx
zelsky, 2015-09-10 10:07:11

nginx django redirect without www?

The site works only on a domain without www .C www Server not found .
And just rewrite 301 didn't help. PCRE Installed. the link in sites-enabled works.
edit in sites-available.
Here is the config

server {
    listen 80;
    server_name domain.net;
 if ($host = 'www.domain.net' ) {
   rewrite  ^/(.*)$  http://domain.net/$1  permanent;
  }


    location /static{
        root /opt/asd/asd/;
    }
    gzip on;
    gzip_disable "msie6";
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript;
    location / {
        include proxy_params;
        proxy_pass http://unix:/opt/asd/asd.sock;
    }
}

In settings
allowed_hosts = ['.dmain.com','www.dmain.com']

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Z
zelsky, 2015-09-10
@zelsky

Everything is very simple. Left the settings. And I just attached the domain with www to my vps in the dns panel. And everything started working properly
server_name domain.net;
listen 80;

A
Alex Popenkov, 2015-09-10
@Alex_Skorpius

try:

server {
    server_name  www.mydomain.com;
    listen 80;
    return 301 http://mydomain.com$request_uri;
}

or
server {
    server_name  www.mydomain.com;
    listen 80;       
    if ($http_host = www.mydomain.com) {
           rewrite ^(.*)$  http://mydomain.com$1 permanent;
    }
}

or alternatively
server_name site.com www.site.com
listen 1.2.3.4;
if ($host ~* www\.(.*)) {
      set $host_without_www $1;
      rewrite ^(.*)$ http://$host_without_www$1 permanent;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question