后端虚拟服务器

server {
    listen 80;
    server_name www.xxx.com api.xxx.com;

    access_log /srv/www/xxx.com/app/logs/www.xxx.com_access.log;
    error_log /srv/www/xxx.com/app/logs/www.xxx.com_error.log notice;

    rewrite ^/app\.php/?(.*)$ /$1 permanent;
    #rewrite ^/(.*)/$ /$1 permanent;

    root   /srv/www/xxx.com/web;
    error_page 500 502 504 /500.html;


    #location ~ .*\.(js|css|gif|jpg|jpeg|png|bmp|swf|ico)$
    #{
    #    add_header Cache-Control "public";
    #    expires 30d;
    #    access_log off;
    #}

    location ~ .*\.(js|css|gif|jpg|jpeg|png|bmp|swf|ico)$
    {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Host $http_host;
        proxy_pass_header Set-Cookie;
        proxy_pass http://10.105.11.135;
    }


    location / {
        index app.php;              #当匹配为 / 时,那么index会去寻找后边的文件在root文件夹下,并出发一个重定向跳转,到其它的location中去处理
        try_files $uri @rewriteapp; #当其它所有模块无法匹配时, 会重新定向到@rewriteapp模块中
    }

    location @rewriteapp {
        rewrite ^(.*)$ /app.php/$1 last;   #last:stops processing the current set of ngx_http_rewrite_module directives and starts a search for a new location matching the changed URI;
    }

    location ~ \.php {
        fastcgi_cache fcgi;                         #这里要指向缓存的key空间
        fastcgi_cache_valid 200 302 1m;             #只缓存200和302的页面一分钟
        fastcgi_cache_valid 404 500 502 503 504 0s; #这里提到的状态码的页面不缓存
        fastcgi_cache_valid any 1m;                 #所有的都缓存1分钟
        fastcgi_cache_min_uses 1;                   #用一次就缓存
        fastcgi_cache_use_stale error timeout invalid_header http_500 http_503 updating; #替换条件
        fastcgi_buffer_size 64k;                    #当从fastcgi server读取数据时要选将内容缓存一下,这个缓存区的大小通常为一个内存页
        fastcgi_buffers 4 64k;                      #缓存数量
        fastcgi_busy_buffers_size 128k;             #用于限制向用户发送页面时使用的缓存空间,以方便使用缓存空间从fast_cgi server接收数据

        fastcgi_pass  unix:/var/run/php/php7.0-fpm.sock;    #传给fpm
        fastcgi_split_path_info ^(.+\.php)(/.*)$;           #该正则表达式,或者两个值1:$fastcgi_script_name,uri中的文件路径。php文件后的参数,2:PATH_INFO
        include /etc/nginx/fastcgi_params;
        fastcgi_param SCRIPT_FILENAME    $document_root$fastcgi_script_name;   #用根目录拼接脚本路径,获得文件完整路径
        fastcgi_param HTTPS        off;
    }

}

 

try_files指令

语法:try_files file ... uri 或 try_files file ... = code
默认值:无
作用域:server location

其作用是按顺序检查文件是否存在,返回第一个找到的文件或文件夹(结尾加斜线表示为文件夹),如果所有的文件或文件夹都找不到,会进行一个内部重定向到最后一个参数。

需要注意的是,只有最后一个参数可以引起一个内部重定向,之前的参数只设置内部URI的指向。最后一个参数是回退URI且必须存在,否则会出现内部500错误。命名的location也可以使用在最后一个参数中。与rewrite指令不同,如果回退URI不是命名的location那么$args不会自动保留,如果你想保留$args,则必须明确声明。

location指令

语法:location [=|~|~*|^~|@] /uri/ { … }
默认值:无
作用域:server

location指令是用来为匹配的URI进行配置,URI即语法中的"/uri/",可以是字符串或正则表达式。但如果要使用正则表达式,则必须指定前缀。 [@] 即是命名location,一般只用于内部重定向请求。

posted @ 2016-07-15 00:03  S大好人S  阅读(229)  评论(0编辑  收藏  举报