nginx-reverse-proxy-confnginx-reverse-proxy-conf

 

nginx确实是神器,支持高并发,反向代理,以及各种拓展。

有空要去玩玩lua+nginx,看看nginx底层原理,写写脚本。

 


 

 

0x01.nginx重定向配置

关于nginx,还是习惯lnmp一键安装,假设我nginx安装在 /usr/local/nginx 里面。

直接用lnmp写好的命令添加下,就好了。

基础配置

sudo /root/vhost.sh

原文配置是这样的。

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

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,各种格式图片,静态文件的一些解析方式,过期时间。

当然了,我们还可以自己配置想要的重定向方式。

 

重定向配置

假如,我写了个解析路由的网站,我想把所有链接重定向到 /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的道理是一样的,只要把路由用正则表达配置好,再写点规则就好了。

 


 

 

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;
    }
}

 

下面是我添加防盗图后的结果。

防盗图防盗图

 


 

 via。http://homeway.me/2014/10/28/nginx-reverse-proxy-conf/

最后修改:2017 年 07 月 12 日 08 : 14 AM