Tricks and Tips about Systems/Network

October 20, 2010

NGINX : http domain requests to www

Filed under: admins,Nginx — Liju Mathew @ 6:48 am

How do I re-direct the http domain request www domain request in Nginx server.

You need to add two virtualhost. One is for re-directing non-www request and other one is for server www-domain request.

# [root@u15382543 ~]# vi /etc/nginx/conf.d/domain.com.conf
server {
server_name domain.com;
listen 74.28.17.98:80;
rewrite ^(.*) http://www.domain.com$1 permanent;
}
server {
server_name www.example.com;
}

My domain.conf file is,

server {
server_name domain.com;
listen 74.28.17.98:80;
rewrite ^(.*) http://www.domain.com$1 permanent;
}
server {
listen 74.20.17.98:80;
server_name www.domain.com;
#access_log /var/log/httpd/nginx.access.domain.com.log;

error_log /var/log/httpd/nginx.errors.domain.com.log warn
large_client_header_buffers 4 8k; # prevent some 400 errors
root /var/www/vhosts/domain.com/httpdocs;
index index.php index.html;

location / {
expires 30d;
error_page 404 = @joomla;
log_not_found off;
}

location @joomla {
rewrite ^(.*)$ /index.php?q=$1 last;
}

error_page 500 502 503 504 404 /404.html;
location = /404.html {
root /var/www/vhosts/domain.com/httpdocs/;

location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/vhosts/domain.com/httpdocs$fastcgi_script_name;
include fastcgi_params;
}
# Select files to be deserved by nginx
location ~* ^.+.(swf|zip|rar|html|htm|pdf)$ {
#location ~* ^.+.(zip|rar|html|htm|pdf)$ {
root /var/www/vhosts/domain.com/httpdocs/;
expires 7d;
}
}

Verify the config. file.

[root@u15382543 ~]# /etc/init.d/nginx configtest
2010/10/19 13:43:52 [info] 21005#0: the configuration file /etc/nginx/nginx.conf syntax is ok
2010/10/19 13:43:52 [info] 21005#0: the configuration file /etc/nginx/nginx.conf was tested successfully

Check the error file

#tail -f /var/log/nginx/error.log

Advertisement

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Theme: Rubric. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.