nginx常用配置
2018/12/14    
########### 每个指令必须有分号结束。#################
#user administrator administrators;  #配置用户或者组,默认为nobody nobody。
#worker_processes 2;  #允许生成的进程数,默认为1
#pid /nginx/pid/nginx.pid;   #指定nginx进程运行文件存放地址
error_log log/error.log debug;  #制定日志路径,级别。这个设置可以放入全局块,http块,server块,级别以此为:debug|info|notice|warn|error|crit|alert|emerg
events {
    accept_mutex on;   #设置网路连接序列化,防止惊群现象发生,默认为on
    multi_accept on;  #设置一个进程是否同时接受多个网络连接,默认为off
    #use epoll;      #事件驱动模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport
    worker_connections  1024;    #最大连接数,默认为512
}
http {
    include       mime.types;   #文件扩展名与文件类型映射表
    default_type  application/octet-stream; #默认文件类型,默认为text/plain
    #access_log off; #取消服务日志    
    log_format myFormat '$remote_addr–$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for'; #自定义格式
    access_log log/access.log myFormat;  #combined为日志格式的默认值
    sendfile on;   #允许sendfile方式传输文件,默认为off,可以在http块,server块,location块。
    sendfile_max_chunk 100k;  #每个进程每次调用传输数量不能大于设定的值,默认为0,即不设上限。
    keepalive_timeout 65;  #连接超时时间,默认为75s,可以在http,server,location块。

    upstream mysvr {   
      server 127.0.0.1:7878;
      server 192.168.10.121:3333 backup;  #热备
    }
    error_page 404 https://www.baidu.com; #错误页
    server {
        keepalive_requests 120; #单连接请求上限次数。
        listen       4545;   #监听端口
        server_name  127.0.0.1;   #监听地址       
        location  ~*^.+$ {       #请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小写。
           #root path;  #根目录
           #index vv.txt;  #设置默认页
           proxy_pass  http://mysvr;  #请求转向mysvr 定义的服务器列表
           deny 127.0.0.1;  #拒绝的ip
           allow 172.18.5.54; #允许的ip           
        } 
    }
}

上面是nginx的基本配置,需要注意的有以下几点:


1、1.$remote_addr 与$http_x_forwarded_for 用以记录客户端的ip地址; 2.$remote_user :用来记录客户端用户名称; 3.$time_local : 用来记录访问时间与时区;4.$request : 用来记录请求的url与http协议;


5.$status : 用来记录请求状态;成功是200, 6.$body_bytes_s ent :记录发送给客户端文件主体内容大小;7.$http_referer :用来记录从那个页面链接访问过来的; 8.$http_user_agent :记录客户端浏览器的相关信息;


2、惊群现象:一个网路连接到来,多个睡眠的进程被同事叫醒,但只有一个进程能获得链接,这样会影响系统性能。


3、每个指令必须有分号结束。


一个具体配置

nginx.conf

#  power by www.php.cn
#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    upstream phpfastcgi_proxy {
    server 127.0.0.1:9002;
    server 127.0.0.1:9004;
    server 127.0.0.1:9005;
    server 127.0.0.1:9006;
    server 127.0.0.1:9007;
    }
    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;
    #tcp_nodelay on;
  fastcgi_connect_timeout 300;
  fastcgi_send_timeout 300;
  fastcgi_read_timeout 300;
  fastcgi_buffer_size 128k;
  fastcgi_buffers 4 128k;
  fastcgi_busy_buffers_size 256k;
  fastcgi_temp_file_write_size 256k;

  #gzip  on;
  gzip on;
  gzip_min_length  1k;
  gzip_buffers     4 32k;
  gzip_http_version 1.1;
  gzip_comp_level 2;
  gzip_types       text/plain application/x-javascript text/css application/xml;
  gzip_vary on;
  gzip_disable "MSIE [1-6].";

  server_names_hash_bucket_size 128;
  client_max_body_size     100m; 
  client_header_buffer_size 256k;
  large_client_header_buffers 4 256k;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
        root    "C:/phpstudy/PHPTutorial/WWW";
        location / {
            index  index.html index.htm index.php l.php;

        }
        location /add/ {
            allow   127.0.0.1;
            deny    all;
            #autoindex  on;
        }       
	location /core/ {
            allow   127.0.0.1;
            deny    all;
            #autoindex  on;
        }	
	location /data/ {
            allow   127.0.0.1;
            deny    all;
            #autoindex  on;
        }
	location /include/ {
            allow   127.0.0.1;
            deny    all;
            #autoindex  on;
        }
        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php(.*)$  {
            fastcgi_pass   phpfastcgi_proxy;
            fastcgi_index  index.php;
            fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  PATH_INFO  $fastcgi_path_info;
            fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
            include        fastcgi_params;
            autoindex  off;
                        allow   127.0.0.1;
                        deny    all;
                        #autoindex  on;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443;
    #    server_name  localhost;

    #    ssl                  on;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_timeout  5m;

    #    ssl_protocols  SSLv2 SSLv3 TLSv1;
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers   on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
	# HTTPS server
	server{
                listen 443;
                server_name www.server.com server.com;
                root "c:/web/serverpath/";#站点目录
                ssl on;
                ssl_certificate "C:/phpstudy/PHPTutorial/nginx/conf/ssl/www.server.com.crt";
                ssl_certificate_key "C:/phpstudy/PHPTutorial/nginx/conf/ssl/www.server.com.key";
                ssl_session_timeout 5m;
                ssl_protocols TLSv1 TLSv1.1 TLSv1.2;#按照这个协议配置
                ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;#按照这个套件配置
                ssl_prefer_server_ciphers  on;
		include "c:/web/serverpath/.htaccess";
                location / {
                            index index.html index.htm index.php;
		}
        	location ~ \.php(.*)$ {
            		fastcgi_pass   phpfastcgi_proxy;
            		fastcgi_index  index.php;
            		fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
            		fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            		fastcgi_param  PATH_INFO  $fastcgi_path_info;
            		fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
            		include        fastcgi_params;
        	}
	}
include vhosts.conf;

}

vhost.conf

server {
        listen       80;
        server_name  www.server.com server.com;
        root   "C:\web\serverpath";
        location / {
            index  index.html index.htm index.php;
            #autoindex  on;
        }
        location /add/ {
            allow   127.0.0.1;
            deny    all;
            #autoindex  on;
        }       
	location /core/ {
            allow   127.0.0.1;
            deny    all;
            #autoindex  on;
        }	
	location /data/ {
            allow   127.0.0.1;
            deny    all;
            #autoindex  on;
        }
	location /include/ {
            allow   127.0.0.1;
            deny    all;
            #autoindex  on;
        }
        
	# rewrite ^(.*) https://www.server.com$1 permanent;
	        location ~ /tv.php(.*)$ {
	        # 配置局部可以不用https协议
                index index.php;
                fastcgi_pass   127.0.0.1:9002;
                        fastcgi_index  index.php;
                        fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
                        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                        fastcgi_param  PATH_INFO  $fastcgi_path_info;
                        fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
                        include        fastcgi_params;
                }
        location / {
            rewrite ^(.*) https://www.qk6080.com$1 permanent;
         }
        location ~ \.php(.*)$ {
            fastcgi_pass   phpfastcgi_proxy;
            fastcgi_index  index.php;
            fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  PATH_INFO  $fastcgi_path_info;
            fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
            include        fastcgi_params;
        }
}
server {
        listen       8016;
        server_name  server_localhost ;
        root   "C:\web\serverpath";
        location / {
            index  index.html index.htm index.php;
            #autoindex  on;
        }
        location /add/ {
            allow   127.0.0.1;
            deny    all;
            #autoindex  on;
        }       
	location /core/ {
            allow   127.0.0.1;
            deny    all;
            #autoindex  on;
        }	
	location /data/ {
            allow   127.0.0.1;
            deny    all;
            #autoindex  on;
        }
	location /include/ {
            allow   127.0.0.1;
            deny    all;
            #autoindex  on;
        }
        location ~ \.php(.*)$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  PATH_INFO  $fastcgi_path_info;
            fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
            include        fastcgi_params;
        }
}

server {
        listen       8017;
        server_name  s_localhost1 ;
        root   "C:\web\serverpath";
        location / {
            index  index.html index.htm index.php;
            #autoindex  on;
        }
        location /add/ {
            allow   127.0.0.1;
            deny    all;
            #autoindex  on;
        }       
	location /core/ {
            allow   127.0.0.1;
            deny    all;
            #autoindex  on;
        }	
	location /data/ {
            allow   127.0.0.1;
            deny    all;
            #autoindex  on;
        }
	location /include/ {
            allow   127.0.0.1;
            deny    all;
            #autoindex  on;
        }
        location ~ \.php(.*)$ {
            fastcgi_pass   127.0.0.1:9001;
            fastcgi_index  index.php;
            fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  PATH_INFO  $fastcgi_path_info;
            fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
            include        fastcgi_params;
        }
}

.htaccess

User-agent: *
Disallow: /add/
Disallow: /core/
Disallow: /data/
Disallow: /include/
Disallow: /template/
Disallow: /xiaosenlin/
Disallow: /styles/
Disallow: /scripts/
Disallow: /images/
Disallow: /playparse2.php
Sitemap: https://www.server.com/google.xml

参考https://www.cnblogs.com/knowledgesea/p/5175711.html