关于nginx,仍是习惯lnmp一键安装,假设我nginx安装在 /usr/local/nginx
里面。css
直接用lnmp写好的命令添加下,就行了。html
sudo /root/vhost.shnginx
原文配置是这样的。正则表达式
保存在 /user/local/nginx/conf/vhost/ooxx.com.conf
apache
server { listen 80; #listen [::]:80; server_name ooxx.com; index index.html index.htm index.php default.html default.htm default.php; root /home/wwwroot/homeway.me; include router.conf; # 添加的路由重定向配置 #error_page 404 /404.html; # 配置php,解析到php cgi location ~ [^/]\.php(/|$) { # comment try_files $uri =404; to enable pathinfo try_files $uri =404; fastcgi_pass unix:/tmp/php-cgi.sock; fastcgi_index index.php; include fastcgi.conf; #include pathinfo.conf; } # 配置图片过时时间 location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; } # 配置静态文件过时时间 location ~ .*\.(js|css)?$ { expires 12h; } access_log /home/wwwlogs/homeway.me.log access; }
从上面能够看出,这些都是基础配置,配置了.php,各类格式图片,静态文件的一些解析方式,过时时间。并发
固然了,咱们还能够本身配置想要的重定向方式。dom
假如,我写了个解析路由的网站,我想把全部连接重定向到 /index.php 这个文件再作路由解析,试试下面的。高并发
保存在 /user/local/nginx/conf/router.conf
网站
location / { index index.html index.php; if (-f $request_filename/index.html){ rewrite (.*) $1/index.html break; } if (-f $request_filename/index.php){ rewrite (.*) $1/index.php; } if (!-f $request_filename){ rewrite (.*) /index.php; } }
$request_filename
就是访问的根目录,这里对根目录作了重定向,将全部连接重写到 index.php 这个文件里,咱们就能够在这里作路由了。
很明显,关于apache中.htaccess的重定向转化到nginx的道理是同样的,只要把路由用正则表达配置好,再写点规则就行了。
若是要对mo
基本也不要解释。
upstream jenkins { # 均衡负载 server 127.0.0.1:8080 fail_timeout=0; } server { listen 80; return 301 https://$host$request_uri; } server { listen 443; server_name jenkins.domain.tld; ssl on; ssl_certificate /etc/nginx/ssl/server.crt; ssl_certificate_key /etc/nginx/ssl/server.key; location / { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_redirect http:// https://; proxy_pass http://homeway.me; #指向代理网站 } }
防盗图主要是针对定向的路径,当用户访问该路径,检测域名,若是域名不对,重定向。
因此先要写个正则表达式。
假设,个人路径是 /public/img/xxx.jpg
那么我就能够这样写。
location ~ \/public\/(css|js|img)\/.*\.(js|css|gif|jpg|jpeg|png|bmp|swf) { valid_referers none blocked *.homeway.me; if ($invalid_referer) { rewrite ^/ http://xiaocao.u.qiniudn.com/blog%2Fpiratesp.png; } }
下面是我添加防盗图后的结果。
-by小草
2014-10-28 22:27:10