• 每天进步一点点!

文章分类

推荐网站

常用手册

nginx 手机端访问跳转到m站点【原创】

nginx 手机端访问跳转到m站点

     location / {
            if ($http_user_agent ~* (mobile|nokia|iphone|ipad|android|samsung|htc|blackberry)) {
                rewrite ^/(.*)$ http://m.1weidu.com/$1 permanent;    #进行正常访问的时候判断user_agent为手机,则重写至此页面
            }
         try_files $uri /index.php?$args;
     }

 

类别:Nginx | 浏览(107) | 评论(0) | 阅读全文>>

nginx远程PHP开发环境设置【原创】

server {
    listen       80;
    server_name  ~^(?<subdomain>.+)\.dev.domain.com;

    root /data/dev/$subdomain/project;
    index index.php;
    charset utf-8;

    access_log  /var/log/nginx/domain.com-access.log;
    error_log  /var/log/nginx/domain.com-error.log;

    location ~ .*\.(ico|gif|jpg|jpeg|png|bmp|swf)$ {
        expires 30d;
    }

    location ~ .*\.(js|css)$ {
        expires 10d;
    }

    location / {
        try_files $uri /index.php?$args;
    }

    location ~ \.php$ {
        fastcgi_index  index.php;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

 

这样在/data/dev/下新建开发者的目录,可以以开发者名字的全拼或简写命名,如:abc,然后在这个目录acb下开发对应的项目project,项目的访问地址为:abc.dev.domain.com。可以做域名解析或绑定host访问。

类别:Nginx | 浏览(76) | 评论(0) | 阅读全文>>

Ubuntu下Nginx的多虚拟主机配置【转载】

  • 在/etc/nginx/sites-available/目录下新建两个文件

文件名:aaa

内容:

server
{
    listen 80;
    charset utf8;
    server_name www.aaa.com aaa.com;
    root /var/www/nginx/aaa;
    index index.html index.htm index.php;
}

 

文件名:bbb

内容:

server
{
    listen 80;
    charset utf8;
    server_name www.bbb.com bbb.com;
    root /var/www/nginx/bbb;
    index index.html index.htm index.php;
}

 

  • 做链接
ln -s /etc/nginx/sites-available/aaa /etc/nginx/sites-enabled/aaa
ln -s /etc/nginx/sites-available/bbb /etc/nginx/sites-enabled/bbb

这步很重要,否则无法加载虚拟主机的配置文件,并且链接必须是完整路径

  • 做hosts

打开/etc/hosts文件增加下面两行

127.0.0.1   www.aaa.com
127.0.0.1   www.bbb.com
  • 新建目录
mkdir  /var/www/nginx/aaa
mkdir/var/www/nginx/bbb

chmod -R 755 /var/www/nginx/aaa
chmod -R 755 /var/www/nginx/bbb
  • 重启

service nginx restart

类别:Nginx | 浏览(95) | 评论(0) | 阅读全文>>

Nginx下CI的rewrite写法【转载】

修改nginx的虚拟主机配置文件,加入下面的配置,部分配置依据个人情况而定:

server
{
    
	listen       80;
	server_name  www.05do.com;
	index index.html index.htm index.php;
	root  /var/www/html/o5do;

	location / {
		rewrite ^/$ /index.php last;
		rewrite ^/(?!index\.php|public|temp|uploads|robots\.txt)(.*)$ /index.php/$1 last;
	}
	
	location ~ ^(.+\.php)(.*)$
    {
		fastcgi_pass  127.0.0.1:9000;
		fastcgi_index index.php;
		fastcgi_split_path_info ^(.+\.php)(.*)$;
		fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
		fastcgi_param PATH_INFO $fastcgi_path_info;
		fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
		include fastcgi_params;
	}
 
}
类别:Nginx | 浏览(98) | 评论(0) | 阅读全文>>

Nginx的location详解【转载】

语法规则: location [=|~|~*|^~] /uri/ { … }

 

= 开头表示精确匹配

^~ 开头表示uri以某个常规字符串开头,理解为匹配 url路径即可。nginx不对url做编码,因此请求为/static/20%/aa,可以被规则^~ /static/ /aa匹配到(注意是空格)。
~ 开头表示区分大小写的正则匹配
~*  开头表示不区分大小写的正则匹配
!~和!~*分别为区分大小写不匹配及不区分大小写不匹配 的正则
/ 通用匹配,任何请求都会匹配到。
多个location配置的情况下匹配顺序为(参考资料而来,还未实际验证,试试就知道了,不必拘泥,仅供参考):
首先匹配 =,其次匹配^~, 其次是按文件中顺序的正则匹配,最后是交给 / 通用匹配。当有匹配成功时候,停止匹配,按当前匹配规则处理请求。
 

例子,有如下匹配规则:
 

location = / {
  #规则A
}
location = /login {
  #规则B
}
location ^~ /static/ {
  #规则C
}
location ~ \.(gif|jpg|png|js|css)$ {
  #规则D
}
location ~* \.png$ {
  #规则E
}
location !~ \.xhtml$ {
  #规则F
}
location !~* \.xhtml$ {
  #规则G
}
location / {
  #规则H
}

那么产生的效果如下:
访问根目录/, 比如http://localhost/ 将匹配规则A
访问 http://localhost/login 将匹配规则B,http://localhost/register 则匹配规则H
访问 http://localhost/static/a.html 将匹配规则C
访问 http://localhost/a.gif, http://localhost/b.jpg 将匹配规则D和规则E,但是规则D顺序优先,规则E不起作用,而http://localhost/static/c.png 则优先匹配到规则C
访问 http://localhost/a.PNG 则匹配规则E,而不会匹配规则D,因为规则E不区分大小写。
访问 http://localhost/a.xhtml 不会匹配规则F和规则G,http://localhost/a.XHTML不会匹配规则G,因为不区分大小写。规则F,规则G属于排除法,符合匹配规则但是不会匹配到,所以想想看实际应用中哪里会用到。
访问 http://localhost/category/id/1111 则最终匹配到规则H,因为以上规则都不匹配,这个时候应该是nginx转发请求给后端应用服务器,比如FastCGI(php),tomcat(jsp),nginx作为方向代理服务器存在。

类别:Nginx | 浏览(76) | 评论(0) | 阅读全文>>