Linux--nginx域名綁定-url rewrite


進入/usr/local/nginx/conf

編輯 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 中的。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM