Redirect с переменной $host в условии

Alexander Yusupov kekc at sympatico.ca
Fri Aug 26 11:22:33 MSD 2005


Забыл сказать что с $http_host тоже пробовал, т.е.:
if ($http_host ~ !^www\. ){
  rewrite  ^(.*)$  http://www.$http_host$1;
} не работает. Приведу свой конфиг может, что в нем не так. 

Плюс к этому игнорируется директива:
location ~* ^.+\.(swf|GIF|gif|JPG|JPEG|jpg|jpeg|PNG|png|ICO|ico|js|css)$ {
	access_log   off;
	expires      30d;
}







nginx.conf
----------------------
user  www;
worker_processes  15;

error_log  /var/log/nginx/error.log;
pid        /var/run/nginx.pid;


events {
    connections  1024;
	use kqueue;
}


http {
	include       mime.types;
	default_type  text/plain;

	sendfile		on;
	tcp_nopush		on;
	tcp_nodelay		on;


	keepalive_timeout  75 20;

	gzip  on;
	gzip_min_length 1100;
	gzip_types text/plain;

	log_format vhost '%{Host}i %addr - - [%time] "%request" %status %length 
"%{Referer}i" "%{User-Agent}i"';

	server {

		listen 80;
		server_name somename.com;


		root /www/catalog/www;

		location ~* ^.+\.(swf|GIF|gif|JPG|JPEG|jpg|jpeg|PNG|png|ICO|ico|js|css)$ {
			root /www/catalog/www;
			access_log   off;
			expires      30d;
		}


		access_log /var/log/nginx/access.log vhost;


		location / {
			root /www/catalog/www;
			index index.php;
		}

		error_page 404 /;

		# deny access to .htaccess files
		location ~ /\.ht {
			deny  all;
		}

#		location /status { stub_status on; }



		location ~* ^.+\.php$ {
			root /www/catalog/www;
		 
			fastcgi_pass	127.0.0.1:1088;
			fastcgi_index	index.php;

			fastcgi_param	SCRIPT_FILENAME  /www/catalog/www$fastcgi_script_name;
			fastcgi_param	QUERY_STRING     $query_string;
			fastcgi_param	REQUEST_METHOD   $request_method;
			fastcgi_param	CONTENT_TYPE     $content_type;
			fastcgi_param	CONTENT_LENGTH   $content_length;

			# жрет ресурсы, без нужды не использоват
			fastcgi_param	REMOTE_ADDR      $remote_addr;
			fastcgi_param	SERVER_PORT          $server_port;
			fastcgi_param	REDIRECT_STATUS  200;
			 
			fastcgi_param	DOCUMENT_ROOT   /www/catalog/www;
			fastcgi_param	SCRIPT_FILENAME /www/catalog/www$fastcgi_script_name;
			fastcgi_param	PATH_TRANSLATED /www/catalog/www$fastcgi_script_name;
			fastcgi_param	SCRIPT_NAME      $fastcgi_script_name;

			fastcgi_param   QUERY_STRING    $query_string;
			fastcgi_param   CONTENT_TYPE    $content_type;
			fastcgi_param   CONTENT_LENGTH  $content_length;
			fastcgi_param   REDIRECT_STATUS 200; 
			#additional system call is made, without need it is better to not use
			fastcgi_param   SERVER_ADDR     $server_addr; 
			fastcgi_param   SERVER_PORT     $server_port;
			fastcgi_param   SERVER_PROTOCOL $server_protocol;
			fastcgi_param   SERVER_SOFTWARE "nginx";
			fastcgi_param   GATEWAY_INTERFACE       "CGI/1.1";
			fastcgi_param   SERVER_NAME     $server_name;
			fastcgi_param   REQUEST_URI     $request_uri;
			fastcgi_param   REQUEST_METHOD  $request_method;
			fastcgi_param   REMOTE_USER     $remote_user;
			fastcgi_param   REMOTE_ADDR     $remote_addr;
			fastcgi_param   REMOTE_PORT     $remote_port;


		}


		if ($http_host ~ !^www\. ){
			rewrite  ^(.*)$  http://www.$http_host$1;
		}


	}


}

On Friday 26 August 2005 01:26, Igor Sysoev wrote:
> On Fri, 26 Aug 2005, Alexander Yusupov wrote:
> > Есть несколько виртуальных серверов (все описаны в едином разделе
> > server). Требуется следуюшие: проверятся наличие www. в имени домена,
> > если нет, то идет редирект на www.$host
> >
> > в апаче можно сделать так:
> >
> > RewriteCond %{HTTP_HOST}   !^www\. [NC]
> > RewriteCond %{HTTP_HOST}   !^$
> > RewriteRule ^/(.*)         http://www.%{HTTP_HOST}/$1 [L,R=301]
> >
> > в nginx такой способ не работает, покрайней мере у меня:
> > if ($host ~* !^www\.){
> >   rewrite  ^(.*)$  http://www.$host$1;
> > }
> >
> > есть идеи как можно такое сделать?
>
> if ($http_host ~* !^www\.){
>
>
> Игорь Сысоев
> http://sysoev.ru

-- 
Regards, Alexander





More information about the nginx-ru mailing list