目录结构

  1. conf/nginx.conf配置详解
    # 启动子进程默认用户。一般编译时指定nginx用户,若未指定则使用nobody
    #user  nobody;
    # nginx服务进程包含一个主进程和多个工作进程。这里定义工作进程数量
    # 一般与CPU的核心数相关,设置auto则自动根据核心数适配,通常情况下,单核设置1,双核设置2,依此类推。也可以设置为cpu数的两倍
    worker_processes auto;
    # 设置cpu亲和力,在高并发情况下,通过设置cpu亲和力来降低由于多CPU核切换带来的性能损耗。
    # 有多少个核,就有几位数;1表示该核心开启,0表示该核心关闭;开多少进程就组合多少数。可以设值auto,自动设置cpu亲和
    worker_cpu_affinity auto;
    #worker_cpu_affinity 1;
    #worker_cpu_affinity 01 10;
    #worker_cpu_affinity 001 010 100;
    #worker_cpu_affinity 0001 0010 0100 1000;
    # 一个nginx进程打开的最多文件数目。最好与ulimit -n的值保持一致
    #worker_rlimit_nofile 65535;
    # 全局错误日志的位置及日志记录级别
    #error_log  logs/error.log;
    #error_log  logs/error.log  notice;
    #error_log  logs/error.log  info;
    			
    # 指定保存nginx服务的进程id的文件
    #pid        logs/nginx.pid;
    
    # I/O事件配置
    events {
    	# 使用epoll模型,对于2.6以上的内核(uname -r查看内核版本),linux系统中建议使用epoll模型以提高性能
    	use epoll;
    	# 每个工作进程最大的连接数,一般在一万以下,如要设置更大值需配合内核优化。理论上nginx服务最大连接数=worker_processes*worker_connections
    	worker_connections  1024;
    }
    
    http {
    	# 设定mime类型,类型由mime.types文件定义;
    	# 如果不指定,浏览器默认按照纯文本text/plain处理文件
    	include       mime.types;
    	# 默认应用程序文件类型,如果web未设置,文件也没设置时使用该类型
    	default_type  application/octet-stream;
    	# 日志格式
    	# $remote_addr与$http_x_forwarded_for用以记录客户端的ip地址; 
    	# $remote_user:用来记录客户端用户名称; 
    	# $time_local: 用来记录访问时间与时区; 
    	# $request: 用来记录请求的url与http协议; 
    	# $status: 用来记录请求状态;成功是200, 
    	# $body_bytes_sent :记录发送给服务端文件主体内容大小; 
    	# $http_referer:用来记录从哪个页面链接访问过来的; 
    	# $http_user_agent:记录客户浏览器的相关信息;
    	#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指定nginx是否调用sendfile函数(zero copy 方式)来输出文件。
    	# 对于普通应用,建议设为on;对于提供上传下载等服务的IO重负载应用,建议设置为off,以平衡磁盘与网络IO处理速度,降低系统的运算等待时间。
    	# 如果图片显示不正常可以设为off试试
    	sendfile	on;
    	# 此选项允许或禁止使用socket的TCP_CORK选项,仅在开启sendfile的时候使用,防止网络阻塞,积极的减少网络报文段的数量
    	tcp_nopush	on;
    	# 关闭Nagle算法,防止网络拥塞,适用于低延迟场景,如API服务
    	#tcp_nodelay on;
    
    	# 隐藏版本号
    	server_tokens off;
    
    	# 链接超时时间,单位秒
    	#keepalive_timeout  0;
    	keepalive_timeout  65;
    	# 单个长连接最大请求数(默认 100,高并发场景可提升至 1000)
    	#keepalive_requests 100;
    	# 请求头的超时时间,单位秒
    	#client_header_timeout 15;
    	# 请求主体的超时时间,单位秒
    	#client_body_timeout 15;
    	# 限制上传文件大小
    	#client_max_body_size 100m;
    	# 发送数据的超时时间,单位秒
    	#send_timeout 15;
    
    	# 是否开启压缩,开启后客户端请求到的资源都会进行压缩以提升页面资源加载速度;该功能消耗cpu
    	#gzip  on;
    	# 允许压缩的页面最小字节数,建议设置成大于1K
    	#gzip_min_length  1k;
    	# 压缩缓冲区大小,表示申请4个单位为32K的内存作为压缩结果流缓存,默认值是申请与原始数据大小相同的内存空间来存储gzip压缩结果。
    	#gzip_buffers     4 32k;
    	# 压缩版本,用于设置识别HTTP协议版本,默认是1.1
    	#gzip_http_version 1.1;
    	# 压缩比例[1-9],比例越大传输越快,处理越慢,cpu消耗越高
    	#gzip_comp_level 6;
    	# 指定压缩的类型,默认值: text/html,无论是否指定都会压缩
    	#gzip_types text/plain text/css text/xml application/json application/javascript application/xml application/xml+rss;
    	# 启用应答头"Vary: Accept-Encoding"。判断浏览器是否支持压缩
    	#gzip_vary on;
        # 禁用 IE6 以下的 Gzip 压缩(IE6 兼容性问题)
        #gzip_disable "MSIE [1-6]\.";
    
    	# 文件元信息缓存
    	# max=缓存最大元素数,超出则移除最少使用项;inactive=未被访问20秒后移除
    	#open_file_cache          max=10000 inactive=20s;
    	# 缓存有效期。30秒后检查元素是否有效,有更新则更新,无效则移除
    	#open_file_cache_valid    30s;
    	# 最小访问次数。至少访问两次的元素标记为活跃元素,留在缓存中
    	#open_file_cache_min_uses 2;
    	# 对访问文件时发生的错误进行缓存。如果某文件不存在,则下次再次访问相同文件时将直接响应相同的错误,不再查找文件
    	#open_file_cache_errors   on;
    
    	# 后端响应缓存
    	# 定义反向代理缓存区
    	# /usr/local/nginx/cache/proxy = 缓存文件存储路径
    	# levels=1:2 = 缓存目录层级(1级目录+2级子目录,避免单目录文件过多)
    	# keys_zone=proxy_cache:200m = 内存缓存区名称+大小(200MB)
    	# inactive=1h = 1h未访问的缓存自动清理
    	# max_size=20g = 磁盘缓存最大容量(20GB)
    	#proxy_cache_path /usr/local/nginx/cache/proxy levels=1:2 keys_zone=proxy_cache:200m inactive=1h max_size=20g;
    
    	# fastcgi配置,通常用于php
    	# 连接到后端FastCGI的超时时间
    	#fastcgi_connect_timeout 300;
    	# 向FastCGI传送请求的超时时间
    	#fastcgi_send_timeout 300;
    	# 接收FastCGI应答的超时时间
    	#fastcgi_read_timeout 300;
    	# 读取FastCGI应答头需要用多大的缓冲区
    	#fastcgi_buffer_size 64k;
    	# 本地需要用多少和多大的缓冲区来缓冲FastCGI的应答请求
    	#fastcgi_buffers 4 64k;
    	# 繁忙时的buffer,建议设置为fastcgi_buffer的两倍
    	#fastcgi_busy_buffers_size 128k;
    	# 在写入fastcgi_temp_path时将用多大的数据块,默认值是fastcgi_buffers的两倍。该数值设置小时若负载上来时可能报502 Bad Gateway
    	#fastcgi_temp_file_write_size 128k;
    	# 缓存临时目录
    	#fastcgi_temp_path /data/ngx_fcgi_tmp;
    	# 开启FastCGI缓存并为其指定一个名称
    	#fastcgi_cache ngx_fcgi_cache;
    	# 指定应答代码的缓存时间,示例中的值表示将200和302应答缓存一小时,要和fastcgi_cache配合使用
    	#fastcgi_cache_valid 200 302 1h;
    	# 将301应答缓存一天
    	#fastcgi_cache_valid 301 1d;
    	# 将其他应答缓存为1分钟
    	#fastcgi_cache_valid any 1m;
    	# 设置经过多少次请求的相同URL将被缓存。
    	#fastcgi_cache_min_uses 1;
    	# 设置web缓存的Key值,nginx根据Key值md5哈希存储.一般根据$host(域名)、$request_uri(请求的路径)等变量组合
    	#fastcgi_cache_key http://$host$request_uri;
    	# 指定FastCGI服务器监听端口与地址。
    	#fastcgi_pass 127.0.0.1:9000;
    	# 缓存的路径
    	#fastcgi_cache_path /data/ngx_fcgi_cache levels=1:2 keys_zone= ngx_fcgi_cache:128m inactive=1d max_size=10g;
    	
    	server {
    		# 监听端口
    		listen       80;
    		# 主机名称,一般写域名
    		server_name  localhost;
    
    		# 设置字符集
    		#charset koi8-r;
    
    		# 设置主机访问日志的保存路径和格式
    		#access_log  logs/host.access.log  main;
    
    		# 路径配置[=|~|~*|^~] pattern
    		# = 表示做精确匹配,即要求请求的地址和匹配路径完全相同
    		# ~ 正则匹配,区分大小写
    		# ~* 正则匹配,不区分大小写
    		# ^~ 指令用于字符前缀匹配。例如:location ^~ /images/ {…}
    		# 优先级:精确匹配 > 前缀匹配 > 正则匹配 > 正常匹配 > 全匹配
    		location / {
    			# 根目录路径,可以是相对路径(相对于nginx安装目录),也可以是绝对路径
    			root   html;
    			# 索引页面,指定root配置的路径下的某个页面;可以写多个页面,nginx找到哪个就将响应哪个
    			index  index.html index.htm;
    		}
    		
    		# 反向代理
    		#location /proxy {
    			#proxy_pass http://127.0.0.1:8080;
    			#proxy_set_header Host $host;
    			#proxy_set_header X-Real-IP $remote_addr;
    			
    			# 反向代理缓存规则
    			#proxy_cache proxy_cache;
    			#proxy_cache_valid 200 304 10m;  # 成功响应缓存 10 分钟
    			#proxy_cache_key $host$uri$is_args$args;  # 缓存键:主机+URI+参数
    			#proxy_cache_min_uses 2;         # 同一请求被访问 2 次后才缓存(避免缓存低频请求)
    			#add_header X-Cache $upstream_cache_status;
    		#}
    		
    		# stub_status模块提供的状态统计信息
    		location /nginx-status {
    			stub_status on;	# 启用状态监控
    			#allow 127.0.0.1;	# 仅允许本地访问(安全考虑)
    			#deny all;	# 拒绝其他IP访问
    		}
    			
    		#location ~ .*\.(jpg|jpeg|png|js|css)?$ {
    			# 缓存时间
    			#expires      30d;
    			# 关闭访问日志记录
    			access_log off;
    			# 关闭错误日志记录
    			error_log /dev/null;
                add_header Cache-Control "public, max-age=2592000";
    			# 开启实体标签。当资源发生变化时,etag值也会改变,客户端可借此判定是否请求最新资源
                etag on;
    		#}
    		
    		# 防盗链配置(仅允许本站和信任域名访问图片)
    		location ~ .*\.(jpg|jpeg|png|gif)?$ {
    		    root html;
    		    # 允许的 Referer(空 Referer 表示直接访问,如浏览器地址栏输入 URL)
    		    valid_referers none blocked localhost *.mengruo.top;
    		    # 非法 Referer 处理(返回 403 或跳转至默认图片)
    		    if ($invalid_referer) {
    		        return 403;
    		        # 或 return 302 http://yourdomain.com/default.png;
    		    }
    		}
    
    		# 当设置的文件找不到时的提示页面
    		#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
    		# 配置代理。此处是将PHP脚本代理到 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
    		# 将PHP脚本传递给监听127.0.0.1:9000的FastCGI服务器
    		#location ~ \.php$ {
    		#    root           html;
    		#    fastcgi_pass   127.0.0.1:9000;
    		#    fastcgi_index  index.php;
    		#    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    		#    include        fastcgi_params;
    		#}
    			
    		# deny access to .htaccess files, if Apache's document root
    		# concurs with nginx's one
    		# 访问控制。此处为拒绝所有客户端访问.ht文件
    		#location ~ /\.ht {
    			# 允许1.0网段IP访问
    			#allow 192.168.1.0/24;
    			# 拒绝192.168.1.254访问
    			#if ( $remote_addr = 192.168.1.254 ) {
    			#    return 404;
    			#}
    		#    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
    	# 配置https协议
    	#server {
    	#    listen       443 ssl;
    		# SSL证书绑定的域名
    	#    server_name  localhost;
    
    		# SSL证书文件路径,一般放置在nginx/conf/cert/cert.pem,可以填写相对路径cert/cert.pem
    	#    ssl_certificate      cert.pem;
    		# SSL证书私钥文件路径
    	#    ssl_certificate_key  cert.key;
    			
    		# 会话缓存
    	#    ssl_session_cache    shared:SSL:1m;
    		# 会话超时时间
    	#    ssl_session_timeout  5m;
    		# 自定义设置使用的TLS协议的类型以及加密套件(以下为配置示例,请您自行评估是否需要配置)
    		# TLS协议版本越高,HTTPS通信的安全性越高,但是相较于低版本TLS协议,高版本TLS协议对浏览器的兼容性较差。
    	#    ssl_ciphers  HIGH:!aNULL:!MD5;
    		# 表示优先使用服务端加密套件。默认开启
    	#    ssl_prefer_server_ciphers  on;
    
    	#    location / {
    	#        root   html;
    	#        index  index.html index.htm;
    	#    }
    	#}
    
    }
    			
  2. 配置本地的域名解析-仅自己访问,外网不可访问

    客户端是Linux系统修改/etc/hosts文件

    客户端是Windows系统修改c:\windows\system32\drivers\etc\host(需要超级管理员权限,建议使用Notepad++文本编辑工具)