Nginx反向代理、防盜圖

http://homeway.mephp

nginx確實是神器,支持高併發,反向代理,以及各類拓展。

有空要去玩玩lua+nginx,看看nginx底層原理,寫寫腳本。




0x01.nginx重定向配置

關於nginx,仍是習慣lnmp一鍵安裝,假設我nginx安裝在 /usr/local/nginx 裏面。css

直接用lnmp寫好的命令添加下,就行了。html

基礎配置

sudo /root/vhost.shnginx

原文配置是這樣的。正則表達式

保存在 /user/local/nginx/conf/vhost/ooxx.com.confapache

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




0x02.nginx配置ssl

基本也不要解釋。

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; #指向代理網站
    }
}




0x03.nginx防盜圖配置

防盜圖主要是針對定向的路徑,當用戶訪問該路徑,檢測域名,若是域名不對,重定向。

因此先要寫個正則表達式。

假設,個人路徑是 /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;
    }
}


下面是我添加防盜圖後的結果。

防盜圖




本文出自 夏日小草,轉載請註明出處:http://homeway.me/2014/10/28/nginx-reverse-proxy-conf/


-by小草

2014-10-28 22:27:10

相關文章
相關標籤/搜索