Linux--nginx域名绑定-url rewrite

进入/usr/local/nginx/confphp

编辑 nginx.conf
绑定域名:
添加一个 server元素,更改后的配置内容可能以下:
server
{
listen        80;
server_name xmdm.easymobi.cn;
index index.html index.htm index.php;
root  /home/wwwroot;
location ~ .*\.(php|php5)?$
{
fastcgi_pass  unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fcgi.conf;
}
location /status {
stub_status on;
access_log    off;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires       30d;
}
location ~ .*\.(js|css)?$
{
expires       12h;
}
log_format  access  '$remote_addr - $remote_user [$time_local] "$request" '
              '$status $body_bytes_sent "$http_referer" '
              '"$http_user_agent" $http_x_forwarded_for';
access_log  /home/wwwlogs/xmdm.log  access;
}
这里,就将域名:xmdm.easymobi.cn 绑定到该机器上.若是要绑定另一个,则再添加一个server.
添加本身的 server 时要注意的是: log_format access  后的格式不能是同样。否则会报错。
 
url - rewrite:
常常看到以下连接:
http://www.ssss.com/3/4/cmd
这个 http 请求通过 web 服务器时,会被rewrite 规则解析成另外一个地址,如:http://www.ssss.com/index.php?a=3&b=4&c=cmd
若是没有定义rewrite规则,上面的地址确定就没法找到了。
nginx的定义方法和apache的很是类似,以下:
location / {
rewrite ^/category/(\d+)/(.+)/(\d+)$ /category.php?cateId=$1&nowPage=$3 last;
rewrite ^/detail/(\d+)/(.+)$ /detail.php?id=$1 last;
rewrite ^/result/(.+)/(\d+)/(\d+)$ /result.php?keyword=$1&id=$2&nowPage=$3 last;
rewrite ^/result/(\d+)/(\d+)$ /result.php?id=$1&nowPage=$2 last;
}
location / 表示访问域名根目录下的地址
下面rewrite 后有两个正则表达式,前面一个是用户输入的地址;后面是要转义成的。
编辑完成后,须要从新加载 nginx:
进入usr/local/nginx/sbin
执行: ./nginx -s reload
 
下面贴一个 apache 下的设置方式:
<IfModule mod_rewrite.c>
RewriteEngine on 
RewriteRule ^/?category/(\d+)/(.+)/(\d+)$ category.php?cateId=$1&nowPage=$3 [L]
RewriteRule ^/?detail/(\d+)/(.+)$ detail.php?id=$1 [L]
RewriteRule ^/?result/(.+)/(\d+)/(\d+)$ result.php?keyword=$1&id=$2&nowPage=$3 [L]
RewriteRule ^/?result/(\d+)/(\d+)$ result.php?id=$1&nowPage=$2 [L]
</IfModule>
这一段我是放在 .htaccess 中的。
相关文章
相关标签/搜索