月度归档:2010年11月

Nginx之 – Rewrite

都知道使用apache的时候可以使用.htaccess进行为静态的重定向配置。

nginx也可以,单略有不同

首先在

/etc/nginx/nginx.conf文件内
相应的server{} 中加入以下代码,然后我们就可以在nginx.htaccess中编写rewrite规则了。
include nginx.htaccess;

ngixn rewrite 的相关指令有:if rewrite set return break等。
看个简单的例子:

if (host ~* ^(.*?)\.sgfblog\.com) {
set var_host '1';
}
if (host ~* ^192\.168\.1\.(.*?)) {
setvar_host '1';
}
if (host ~* ^localhost) {
setvar_host '1';
}
if (host ~* ^127\.0\.0\.(.*?)) {
set var_host '1';
}
if (var_host !~ '1') {
rewrite ^/(.*)scheme://www.sgfblog.com/$1 redirect;
}

解释一下上述语句的意思:

上面4个if语句都是只有有一个成立就会使用set 把一个变量设置成1
最后1个语句 只要这个变量为1,就执行重定向。

host 指的是主机名,也就是访问网址。

如果主机名不是*.sgfblog.com也不是192.168.1.*也不是localhost也不是127.0.0.*
那么将执行跳转。

rewrite ^/(.*) scheme://www.sgfblog.com/1 redirect;
/(.*)可以匹配根任意字符路径,scheme://www.sgfblog.com/1 则是被重定向到地址scheme 代表协议 可以说http 或 https等
$1 代表匹配时第1处符合的字符串 ,也就是原路径。

redirect代表302重定向,会在url头里改变。

if语句解说


1、变量名不能为空值或者0开头
2、可以使用等于号(=),不等于号(!=)来运算
3、可以使用正则 ~*(不区分大小写) 和 ~(区分大小写)进行匹配
4、!~和 !~*表示不匹配
5、-f 和!-f 表示文件是否存在
6、-d 和 !-d 表示目录是否存在
7、-x 和 !-x表示是否可以执行

支持的flag标签有


标签 解释
last apache中的[L]表示完成rewrite
break 终止匹配
permanent 301
redirect 302

继续阅读

nginx之 – https协议,ssl证书配置

一、什么是https

简单的说,他就是安全加强版的http。
二、https的应用

1、网银类支付类平台

2、需要保证数据安全的oa办公平台

3、注册类、用户中心、代理平台

4、开放接口类

5、耍酷类 (我就属于这种类别)

三、SSL证书

1、https需要依靠ssl证书,在服务器和客户端直接进行加密解密数据传输。该证书由ca颁发,可以在证书颁发机构申请或购买,也可以自行制作。

2、证书生成
windows2003下管理工具中“证书颁发机构”可以实现证书制作。
inux下可以使用openssl 命令制作证书。

# openssl genrsa -des3 -out sgfblog.com.key 1024
# openssl req -new -key sgfblog.com.key -out sgfblog.com.csr
# openssl rsa -in sgfblog.com.key -out sgfblog.com.nopass.key

最后一个命令是为了生成不带密码的证书文件。具体过程不再详详述。

四、Nginx配置

Nginx服务器的安装与配置请参考 https://www.sgfblog.com/archives/140

https 采用的默认端口是443,配置如下:

server{
        listen      443;
        server_name  www.sgfblog.com;

        location / {
            root   /var/www/html;
            index  index.php;
        }
        include /var/www/html/nginx.htaccess;

        error_page  404              /404.html;
        location = /404.html {
            root   /usr/share/nginx/html;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }

        location ~ \.php{
            root           /var/www/html;
            fastcgi_pass   www.sgfblog.com:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /var/www/htmlfastcgi_script_name;
            include        fastcgi_params;
        }

        ssl on;
        ssl_certificate /var/www/cert/sgfblog.com.crt;
        ssl_certificate_key /var/www/cert/sgfblog.com.nopass.key;

    }

可以看出上述配置与普通的站点配置有所不同(第2行不同、新增了29-31行)
第2行监听的不再是80端口,而是443了。
ssl on; 指的是开启ssl
ssl_certificate 指的是crt文件的地址
ssl_certificate_key 指的是key文件的地址。

然后重新启动nginx就可以

service nginx restart

需要主要的是key如果是带密码才能引入的,在(重新)启动nginx需要输入密码。所以上文说生成了个nopass的key文件。
如果nginx是开机自动启动的并且key文件需要密码,那么再登陆系统之前就会有控制台让你输入这个密码,比较麻烦。

nginx之 – 安装与配置

1、启用 EPEL repo

# rpm -Uvh http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-4.noarch.rpm

2、安装nginx

yum -y install nginx

3、配置 PHP FastCGI

# yum install php-pear-Net-Socket php-pear php-common php-gd php-devel php php-mbstring php-pear-Mail php-cli php-imap php-snmp php-pdo php-xml php-pear-Auth-SASL php-ldap php-pear-Net-SMTP php-mysql

4、安装spawn-fcg
继续阅读

centos下安装配置subversion

基本步骤:
1、安装必需的subversion
2、创建版本库
3、配置用户和权限
4、钩子和svn常用命令说明

一、安装subversion
在这里我们使用yum来安装subversion,使用以下命令即可完成。

[root@localhost ~]# yum -y install subversion

二、创建版本库

[root@localhost ~]# svnadmin create /var/www/svnroot

如果需要创建子版本库也可以使用 svnadmin create /var/www/svnroot/子版本库名称
的方式进行创建。但是要注意上级版本库中是否有文件夹叫这个名字,不然不重名冲突

三、版本库管理配置
进行刚才创建的版本库目录下的conf目录,可以看到有三个文件。

[root@localhost ~]# cd /var/www/svnroot/conf
[root@localhost conf]# ls
authz  passwd  svnserve.conf

svnserve.conf 这个是版本库的配置文件
passwd 这个是记录用户帐号密码的文件
authz 这个则是记录组、权限和身份验证的文件

1、配置svnserve.conf文件
这里需要设置以下几处
anon-access = none 指定匿名权限,默认为可读,现设置匿名无权限
auth-access = write 用户有写权限
password-db = passwd psswd文件地址 password-db 设置成 ../../passwd 这样可以方便统一管理
authz-db = authz 同上
注意去掉#注释以后配置一定要顶格写,下同。

[root@localhost conf]# vim svnserve.conf
[general]
### These options control access to the repository for unauthenticated
### and authenticated users.  Valid values are "write", "read",
### and "none".  The sample settings below are the defaults.
anon-access = none
auth-access = write
### The password-db option controls the location of the password
### database file.  Unless you specify a path starting with a /,
### the file's location is relative to the conf directory.
### Uncomment the line below to use the default password file.
password-db = passwd
### The authz-db option controls the location of the authorization
### rules for path-based access control.  Unless you specify a path
### starting with a /, the file's location is relative to the conf
### directory.  If you don't specify an authz-db, no path-based access
### control is done.
### Uncomment the line below to use the default authorization file.
authz-db = authz
### This option specifies the authentication realm of the repository.
### If two repositories have the same authentication realm, they should
### have the same password database, and vice versa.  The default realm
### is repository's uuid.
# realm = My First Repository

2、配置passwd
该文件中记录svn用户名密码,以 (帐号 = 密码)或 (帐号 : 密码)的形式进行储存。
多用户之前用换行区分。这里配置了一个用户名为sgf密码为123456的svn 账户。

[root@localhost conf]# vim passwd
### This file is an example password file for svnserve.
### Its format is similar to that of svnserve.conf. As shown in the
### example below it contains one section labelled [users].
### The name and password for each user follow, one account per line.

[users]
# harry = harryssecret
# sally = sallyssecret
sgf = 123456

3、authz文件的配置
[groups]表用于用户组的配置例如
“group1 = sgf,sgf2”这样就将这2个用户方在了group1 组织之中。
建立组是为了方便给一组相同权限的用户分配权限。
[/] 指定是svn的根版本库
版本库目录格式:
[<版本库>:/项目/目录]
@<用户组名> = <权限>
<用户名> = <权限>

[root@localhost conf]# vim authz
### This file is an example authorization file for svnserve.
### Its format is identical to that of mod_authz_svn authorization
### files.
### As shown below each section defines authorizations for the path and
### (optional) repository specified by the section name.
### The authorizations follow. An authorization line can refer to a
### single user, to a group of users defined in a special [groups]
### section, or to anyone using the '*' wildcard.  Each definition can
### grant read ('r') access, read-write ('rw') access, or no access
### ('').

[groups]
# harry_and_sally = harry,sally

# [/foo/bar]
# harry = rw
# * =

# [repository:/baz/fuz]
# @harry_and_sally = rw
# * = r
[/]
sgf = rw
* =

这样svn版本库就算配置完成了。

运行 svn

[root@localhost ~]# svnserve -d -r /var/www/svnroot

继续阅读