ini Laravel Nginx配置

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ini Laravel Nginx配置相关的知识,希望对你有一定的参考价值。

# Read  
#       http://wiki.nginx.org/Pitfalls
#       http://wiki.nginx.org/QuickStart#       
#       http://tautt.com/best-nginx-configuration-for-security/
#
#       Generate your key with: openssl dhparam -out /etc/nginx/ssl/dhparam.pem 2048
#       Generate certificate: sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/nginx/ssl/nginx.key -out /etc/nginx/ssl/nginx.crt
 
server_tokens off;
 
server {
 
    listen [::]:80 default_server;
    listen 80;

    root /usr/share/nginx/html/;
    index index.php;

    # http://www.gnuterrypratchett.com/#nginx
    add_header X-Clacks-Overhead "GNU Terry Pratchett"; 
    
    # mitigate clickjacking
    add_header X-Frame-Options SAMEORIGIN;
    
    location / {        
        # include /etc/nginx/naxsi.rules;
        try_files $uri $uri/ /index.php?q=$uri&$args;
    }
     
    # Do not log favicon.ico requests
    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }
 
    # Do not log robots.txt requests
    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }
 
    location ~* \.(js|css|png|jpg|jpeg|gif|ico|txt|woff)$ {
        expires max;
        log_not_found off;
    }
     
    # Redirect 403 errors to 404 error to fool attackers
    error_page 403 = 404;
 
    # Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).
    # Keep logging the requests to parse later (or to pass to firewall utilities such as fail2ban)
    location ~ /\. {
        deny all;
    }
 
     location ~ \.php$ {

        try_files $uri =404;

        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        if (!-f $document_root$fastcgi_script_name) {
            return 404;
        }

        include         fastcgi_params;
        fastcgi_keep_conn on;
        fastcgi_index   index.php;
        fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param   SERVER_NAME $host;
        fastcgi_pass    unix:/var/run/php5-fpm.sock;
	      fastcgi_read_timeout 600;
        fastcgi_param   PHP_VALUE "memory_limit = 4096M";
#	      fastcgi_pass   127.0.0.1:9001;      #for hiphop
    }

} 

ini 使用nginx缓存并响应Web请求#nginx #php #performance #laravel

#php
<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Filesystem\Filesystem;

class CacheResponse
{
    /**
     * The filesystem instance.
     *
     * @var \Illuminate\Filesystem\Filesystem
     */
    protected $files;

    /**
     * Constructor.
     *
     * @var \Illuminate\Filesystem\Filesystem  $files
     */
    public function __construct(Filesystem $files)
    {
        $this->files = $files;
    }

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $response = $next($request);

        if ($this->shouldCache($request, $response)) {
            $this->cacheResponse($request, $response);
        }

        return $response;
    }

    /**
     * Determines whether the given request/response should be cached.
     *
     * @param  \Illuminate\Http\Response  $response
     * @return bool
     */
    protected function shouldCache($request, $response)
    {
        return $request->isMethod('GET') && $response->getStatusCode() == 200;
    }

    /**
     * Cache the response to a file.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Illuminate\Http\Response  $response
     * @return void
     */
    protected function cacheResponse($request, $response)
    {
        list($path, $file) = $this->getDirectoryAndFileNames($request);

        $this->files->makeDirectory($path, 0775, true, true);

        $this->files->put($path.$file, $response->getContent());
    }

    /**
     * Get the names of the directory and file.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    protected function getDirectoryAndFileNames($request)
    {
        $segments = explode('/', ltrim($request->getPathInfo(), '/'));

        $file = array_pop($segments).'.html';

        return [$this->getCachePath(implode('/', $segments)), $file];
    }

    /**
     * Get the path to the cache directory.
     *
     * @param  string  $path
     * @return string
     */
    protected function getCachePath($path = '')
    {
        return public_path('page-cache/'.($path ? trim($path, '/').'/' : $path));
    }
}


#nginx
location / {
    try_files $uri $uri/ /page-cache/$uri.html /index.php?$query_string;
}


# ref:  https://github.com/laravel/laravel.com/pull/73#issue-77554608
@package https://github.com/JosephSilber/page-cache

以上是关于ini Laravel Nginx配置的主要内容,如果未能解决你的问题,请参考以下文章

ini Nginx配置基于/ api / v1 url路径的多个laravel站点

ini nginx_laravel.conf

ini nginx virtualhost laravel

ini 示例Nginx Laravel服务器块

ini 使用nginx缓存并响应Web请求#nginx #php #performance #laravel

laravel + php cgi + nginx在windows平台下的配置