Maison  >  Article  >  développement back-end  >  nginx comment configurer un exemple de fichier

nginx comment configurer un exemple de fichier

小云云
小云云original
2018-05-22 16:05:581703parcourir

Cet article explique principalement comment configurer un exemple de code de fichier nginx, j'espère qu'il pourra vous aider.

nginx.conf

user  www www; # Nginx的worker进程运行用户以及用户组worker_processes 4; # 启动进程数,通常设置成和cpu的数量相等#worker_processes auto;#以下参数指定了哪个cpu分配给哪个进程,一般来说不用特殊指定。如果一定要设的话,用0和1指定分配方式.#这样设就是给1-4个进程分配单独的核来运行,出现第5个进程是就是随机分配了。#worker_processes 4     #4核CPU #worker_cpu_affinity 0001 0010 0100 1000error_log  /home/wwwlogs/nginx_error.log  crit; # 定义全局错误日志定义类型,[debug|info|notice|warn|crit]pid        /usr/local/nginx/logs/nginx.pid; # PID文件#Specifies the value for maximum file descriptors that can be opened by this process.# 一个nginx进程打开的最多文件描述符数目,理论值应该是最多打开文件数(ulimit -n)与nginx进程数相除,但是nginx分配请求并不是那么均匀,所以最好与ulimit -n的值保持一致。worker_rlimit_nofile 51200;# 工作模式及连接数上限events
    {        # use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]; epoll模型是Linux 2.6以上版本内核中的高性能网络I/O模型,如果跑在FreeBSD上面,就用kqueue模型。
        use epoll; 
        worker_connections 51200; # 单个后台worker process进程的最大并发链接数
        #worker工作方式:串行(一定程度降低负载,但服务器吞吐量大时,关闭使用并行方式)
        multi_accept on;
    }

http
    {        #文件扩展名与文件类型映射表
        include       mime.types;        #默认文件类型
        default_type  application/octet-stream;
        服务器名字的hash表大小
        server_names_hash_bucket_size 128;        #指定来自客户端请求头的hearerbuffer大小
        client_header_buffer_size 32k;        #指定客户端请求中较大的消息头的缓存最大数量和大小。
        large_client_header_buffers 4 32k;        #客户端请求单个文件的最大字节数
        client_max_body_size 50m;

        sendfile   on; #开启高效传输模式。
        #防止网络阻塞
        tcp_nopush on;

        keepalive_timeout 60; # 连接超时时间 ,单位是秒

        tcp_nodelay on;        #FastCGI相关参数是为了改善网站的性能:减少资源占用,提高访问速度。
        fastcgi_connect_timeout 300;
        fastcgi_send_timeout 300;
        fastcgi_read_timeout 300;
        fastcgi_buffer_size 64k;
        fastcgi_buffers 4 64k;
        fastcgi_busy_buffers_size 128k;
        fastcgi_temp_file_write_size 256k;

        gzip on; # 开启gzip压缩
        gzip_min_length  1k; #最小压缩文件大小
        gzip_buffers     4 16k; #压缩缓冲区
        gzip_http_version 1.1; #压缩版本(默认1.1,前端如果是squid2.5请使用1.0)
        gzip_comp_level 2; #压缩等级 1-9 等级越高,压缩效果越好,节约宽带,但CPU消耗大
        gzip_types     text/plain application/javascript application/x-javascript text/javascript text/css application/xml application/xml+rss; #压缩类型,默认就已经包含text/html,所以下面就不用再写了,写上去也不会有问题,但是会有一个warn。
        #前端缓存服务器缓存经过压缩的页面
        gzip_vary on;
        gzip_proxied   expired no-cache no-store private auth;
        gzip_disable   "MSIE [1-6]\.";        #limit_conn_zone $binary_remote_addr zone=perip:10m;
        ##If enable limit_conn_zone,add "limit_conn perip 10;" to server section.

        server_tokens off;
        access_log off;        # 定义日志格式
        log_format up_head '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for" "deviceid: $http_deviceid"'
                        '"$request_body"  $upstream_response_time "$request_time"';


    include vhost/*.conf;
}

vhost/test.conf

server {
    listen 80;
    listen 443 ssl;
    server_name www.test.com;

    ssl_certificate      /nginx/conf/https/www.test.com.crt;
    ssl_certificate_key  /nginx/conf/https/www.test.com.key;
    root   /www/www.test.com;
    index  index.html index.htm index.php;

    location ~^/test.php {
          default_type    application/json;          return 404 '{404}';
          access_log off;
    }

    location ~ ^/test/status(.*)$
    {
        default_type text/html;
        access_log /log/nginx/access/dot.log;        return 200 '';
    }

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
    location ~\.php$ {
            # 如果用到 sock 则值参考 unix:/var/run/php/php7.0-fpm.sock
            fastcgi_pass             unix:/tmp/php-cgi.sock;
            fastcgi_index            index.php;
            fastcgi_param            SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include                  fastcgi_params;
            access_log /log/nginx/access/access.log up_head;
     }

    #静态文件
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
        expires      30d;
    }

    location ~ .*\.(js|css)?$
    {
        expires      12h;
    }

    location ~ /.well-known {
        allow all;
    }

    location ~ /\.
    {
        deny all;
    }

    error_log /log/nginx/error/error.log;
}

Définition de la description du paramètre de format de journal :

    #定义日志的格式。后面定义要输出的内容。
    #1.$remote_addr 与$http_x_forwarded_for 用以记录客户端的ip地址;
    #2.$remote_user :用来记录客户端用户名称;
    #3.$time_local :用来记录访问时间与时区;
    #4.$request  :用来记录请求的url与http协议;
    #5.$status :用来记录请求状态; 
    #6.$body_bytes_sent :记录发送给客户端文件主体内容大小;
    #7.$http_referer :用来记录从那个页面链接访问过来的;
    #8.$http_user_agent :记录客户端浏览器的相关信息

Recommandations associées :

Explication détaillée du fichier de configuration Nginx nginx.conf

Explication détaillée de l'exemple de fichier de configuration Nginx

Configuration Nginx Brève description du fichier

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn