1

My problem is about www and non www urls. I use nginxCP, a distribution of nginx that includes cPanel and WHM.

I need to be able to surf my sites hosted by this server with both non-www and www urls. Ex: when I type www.example.com I surf this site with the www. When I type example.com, I surf this site WITHOUT the www. I don't want a redirect between the two.

I have included my config files for nginx and a site's vhost (that is automatically generated by nginxCP).

/etc/nginx/nginx.config:

user  nobody;
# no need for more workers in the proxy mode
worker_processes  auto;
error_log  /var/log/nginx/error.log warn;
worker_rlimit_nofile 20480;
events {
 worker_connections 5120; # increase for busier servers
 use epoll; # you should use epoll here for Linux kernels 2.6.x
}
http {

 server_name_in_redirect on;
 server_names_hash_max_size 10240;
 server_names_hash_bucket_size 1024;
 include    mime.types;
 default_type  application/octet-stream;
 server_tokens off;
# remove/commentout disable_symlinks if_not_owner;if you get Permission denied error
# disable_symlinks if_not_owner;
 sendfile on;
 tcp_nopush on;
 tcp_nodelay on;
 keepalive_timeout  5;
 gzip on;
 gzip_vary on;
 gzip_disable "MSIE [1-6]\.";
 gzip_proxied any;
 gzip_http_version 1.0;
 gzip_min_length  1000;
 gzip_comp_level  6;
 gzip_buffers  16 8k;
# You can remove image/png image/x-icon image/gif image/jpeg if you have slow CPU
 gzip_types    text/plain text/xml text/css application/x-javascript application/xml application/javascript application/xml+rss text/javascript application/atom+xml;
 ignore_invalid_headers on;
 client_header_timeout  3m;
 client_body_timeout 3m;
 send_timeout     3m;
 reset_timedout_connection on;
 connection_pool_size  256;
 client_header_buffer_size 256k;
 large_client_header_buffers 4 256k;
 client_max_body_size 200M;
 client_body_buffer_size 128k;
 request_pool_size  32k;
 output_buffers   4 32k;
 postpone_output  1460;
 proxy_temp_path  /tmp/nginx_proxy/;
 proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=microcache:15m inactive=24h max_size=500m;
 client_body_in_file_only on;
 log_format bytes_log "$msec $bytes_sent .";
 log_format custom_microcache '$remote_addr - $remote_user [$time_local] '
        '"$request" $status $body_bytes_sent '
        '"$http_referer" "$http_user_agent" nocache:$no_cache';
include "/etc/nginx/vhosts/*";
}

/etc/nginx/vhosts/mysite.com:

server {
          error_log /var/log/nginx/vhost-error_log warn;
          listen xxx.xxx.xxx.xxx:80;
          listen [::]:80;
          server_name www.mysite.com mysite.com;
          access_log /usr/local/apache/domlogs/mysite.com-bytes_log bytes_log;
          access_log /usr/local/apache/domlogs/mysite.com combined;
          root /home/syriosrl/public_html;
          #location / {
          location ~*.*\.(3gp|gif|jpg|jpeg|png|ico|wmv|avi|asf|asx|mpg|mpeg|mp4|pls|mp3|mid|wav|swf|flv|html|htm|txt|js|css|exe|zip|tar|rar|gz|tgz|bz2|uha|7z|doc|docx|$
          expires 1M;
          try_files $uri @backend;
          }
          location / {
          error_page 405 = @backend;
          add_header X-Cache "HIT from Backend";
          proxy_pass http://xxx.xxx.xxx.xxx:8081;
          include proxy.inc;
          include microcache.inc;
          }
          location @backend {
          internal;
          proxy_pass http://xxx.xxx.xxx.xxx:8081;
          include proxy.inc;
          include microcache.inc;
          }
          location ~ .*\.(php|jsp|cgi|pl|py)?$ {
          proxy_pass http://xxx.xxx.xxx.xxx:8081;
          include proxy.inc;
          include microcache.inc;
          }
          location ~ /\.ht {
          deny all;
          }
        }
2
  • 1
    You asked the same question on StackOverflow... Why Nginx redirect www to non-www urls? How to allow both?. Maybe consider migrating the question from SO to here? Commented Feb 19, 2015 at 18:36
  • The question is off topic for StackOverflow. It is likely to be closed there. It is more appropriate here. Because it was cross posted, it is unlikely to me migrated. Commented Feb 19, 2015 at 18:57

1 Answer 1

0

I resolved it after a deep study of my sites. In my server I have two sites: the first is written by me (a managerial software), and the second uses WordPress. I noticed out that the first site can be surfed with www and without-www, so I deduced that the problem wasn't the web server, but the CMS. After some researches on the web about this thing, I found out that WordPress (and also Magento) forces all the urls to be non-www (or the contrary, depending on WP version). I fixed this problem with WP by adding this few line of code at the end of wp-config.php file, in the site's root:

$hostname = $_SERVER['SERVER_NAME'];
define('WP_SITEURL', 'http://' . $hostname);
define('WP_HOME', 'http://' . $hostname);

In Magento I can fix this problem with these methods: https://docs.nexcess.net/article/changing-magento-base-urls.html

Ps: if you want to test the non-CMS site's urls are http://www.merlinimediazioni.com and http://merlinimediazioni.com ; the CMS site's urls are http://www.mattiamerlini.it and http://mattiamerlini.it . When you load one of these urls you can notice that if you have inserted www.mattiamerlini.it the url remain with www, and viceversa for non-www.

Thank you for your time and help

Have a nice weekend

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.