博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
理解nginx的配置
阅读量:7236 次
发布时间:2019-06-29

本文共 3946 字,大约阅读时间需要 13 分钟。

Nginx配置文件主要分成四部分:main(全局设置)server(主机设置)upstream(上游服务器设置,主要为反向代理、负载均衡相关配置)location(URL匹配特定位置后的设置),每部分包含若干个指令。

main部分设置的指令将影响其它所有部分的设置;server部分的指令主要用于指定虚拟主机域名、IP和端口;upstream的指令用于设置一系列的后端服务器,设置反向代理及后端服务器的负载均衡;location部分用于匹配网页位置(比如,根目录"/","/images",等等)。

他们之间的关系式:server继承main,location继承server;upstream既不会继承指令也不会被继承。它有自己的特殊指令,不需要在其他地方的应用。

可以有多个server,location属于server子集。

一个完整的nginx.conf

user  www www;worker_processes  1; # worker角色的工作进程个数,简单可设置CPU核数error_log  /www/log/nginx/error.log crit;pid        /www/server/nginx/logs/nginx.pid;#Specifies the value for maximum file descriptors that can be opened by this process. worker_rlimit_nofile 65535;events {  use epoll;  worker_connections 65535;}# http设置http {    include       mime.types;    default_type  application/octet-stream;    autoindex on;  #自动显示目录    autoindex_exact_size off; #人性化方式显示文件大小否则以byte显示    autoindex_localtime on; #按服务器时间显示,否则以gmt时间显示    # log格式设置    # log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '    #                  '$status $body_bytes_sent "$http_referer" '    #                  '"$http_user_agent" "$http_x_forwarded_for"';    server_names_hash_bucket_size 128;    client_header_buffer_size 32k;    large_client_header_buffers 4 32k;    sendfile on; # 开启高效文件传输模式;下载服务器设置为off        tcp_nopush on; # 默认值即可    tcp_nodelay on; # 默认值即可    keepalive_timeout 60; # 长连接超时时间,单位是秒    send_timeout 20; # 指定响应客户端的超时时间        client_max_body_size 10m;# 允许客户端请求的最大单文件字节数    client_body_buffer_size 128k; # 缓冲区代理缓冲用户端请求的最大字节数    # 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 128k;    # gzip压缩功能设置    gzip on; # 开启gzip压缩输出,减少网络传输    gzip_min_length  1k;    gzip_buffers     4 16k;    gzip_http_version 1.0;    gzip_comp_level 2;    gzip_types       text/plain application/x-javascript text/css application/xml;    gzip_vary on;        # Nginx带宽限制    #limit_zone  crawler  $binary_remote_addr  10m;    # http_proxy设置,按需设置    proxy_connect_timeout   75; # 代理连接超时    proxy_send_timeout   75; # 代理发送超时    proxy_read_timeout   75; # 代理接收超时    proxy_buffer_size   4k;    proxy_buffers   4 32k;    proxy_busy_buffers_size   64k;    proxy_temp_file_write_size  64k;    proxy_temp_path   /www/server/nginx/proxy_temp 1 2;        #利用proxy_cache来缓存文件    #levels设置目录层次     #keys_zone设置缓存名字和共享内存大小     #inactive在指定时间内没人访问则被删除在这里是1天     #max_size最大缓存空间    proxy_cache_path /www/server/nginx/proxy_cache levels=1:2 keys_zone=content:20m inactive=1d max_size=100m;      #等号中间要加的,关键只要加上proxy_cache_path          # 设定负载均衡后台服务器列表,按需设置,一般不需要    upstream  backend  {       #ip_hash;       server   192.168.10.100:8080 max_fails=2 fail_timeout=30s ;        server   192.168.10.101:8080 max_fails=2 fail_timeout=30s ;      }        # 虚拟主机配置,server 指令开始    include /www/server/nginx/conf/vhosts/*.conf;}

完整的vhost的配置文件:

/www/server/nginx/conf/vhosts/me.52fhy.com.conf

#虚拟主机配置server {    listen       80;    server_name  me.52fhy.com;    index index.php index.html index.htm;    root /52fhy.com/wordpress/;        # WordPress Rewrite    location / {        if (-f $request_filename/index.html){            rewrite (.*) $1/index.html break;        }        if (-f $request_filename/index.php){            rewrite (.*) $1/index.php;        }        if (!-f $request_filename){            rewrite (.*) /index.php;        }    }        # 解析php|php5后缀    location ~ .*\.(php|php5)?$    {        #fastcgi_pass  unix:/tmp/php-cgi.sock;        fastcgi_pass  127.0.0.1:9000;        fastcgi_index index.php;        include fastcgi.conf;    }        # 设置gif|jpg|jpeg|png|bmp|swf文件缓存时间    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$    {        expires 30d; # 缓存30天    }        # 设置js|css文件缓存时间    location ~ .*\.(js|css)?$    {        expires 1h; # 缓存1小时    }    # 访问日志    access_log  /www/log/me.52fhy.com.log;}

转载地址:http://gswbm.baihongyu.com/

你可能感兴趣的文章
LVS的DR和NAT模式配置
查看>>
总结概括对于大数据、高并发的网站如何进行优化的问题
查看>>
学习javascript必须订阅30个程序员的Blog
查看>>
如何将Exchange 2007迁移到一台新的服务器并且保留原有服务器名
查看>>
设置控件只读
查看>>
box-align,box-pack实现元素垂直底部对齐
查看>>
poj 2385Apple Catching(简单dp)
查看>>
出现有持续性的Lazy Write
查看>>
bootstrap-辅助类-情境文本颜色
查看>>
Python3类型提示
查看>>
websocket多线程问题
查看>>
从上往下打印二叉树
查看>>
决策支持系统是什么?
查看>>
解压缩
查看>>
Linux中curl命令和wget命令的使用介绍与比较
查看>>
shell之awk 记录
查看>>
python内置数据结构之set
查看>>
function_score 之script_score
查看>>
ssh配置客户端免密钥到服务端
查看>>
启动PHP时提示初始化错误
查看>>