nginx 配置

运行原理:
        master进程,该进程不处理任何用户客户端请求,它用来产生worker进程,它可以定义worker进程及每个worker进程的最大连接数。




默认配置文件    [root@localhost conf]# cd /usr/local/nginx/conf/nginx.conf
Nginx配置文件主要分成四部分:
main(全局设置)、
server(主机设置)、
upstream(上游服务器设置,主要为反向代理、负载均衡相关配置)、
 location(URL匹配特定位置后的设置)、

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

配置文件总体架构
events{  }

http {

      server{
             location{
             }
       }

      upstream{
      }
}

nginx 配置 - 朝鲜程序员 - 朝鲜程序员的博客
 



[root@localhost conf]# cat nginx.conf

#user  nobody;                          用户账户、用户组  定义Nginx运行的用户和用户组
worker_processes  1;              1个工作进程(可以增加数量与cpu核数一致)

#error_log  logs/error.log;                    错误 全局错误日志定义类型  路径
#error_log  logs/error.log  notice;         [ debug | info | notice | warn | error | crit ]
#error_log  logs/error.log  info;

#pid        /var/run/nginx.pid;                            Pid进程文件位置



         events模块的指令可以用来配置网络机制,这些指令参数会对nginx应用程序性能产生重要的影响。
events模块配置应放在配置文件顶部。
events {
    
#参考事件模型,use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]; epoll模型是Linux 2.6以上版本内核中的高性能网络I/O模型,如果跑在FreeBSD上面,就用kqueue模型。
    use epoll;
    worker_connections  1024;      定义一个worker进程同时连接的数量    #单个进程最大连接数(最大连接数=连接数*进程数)
}



          http模块配置nginx相关的web服务
http {
    include       mime.types;                           include包含其他指令文件   设定mime类型,类型由mime.type文件定义
    default_type  application/octet-stream;       默认文件类型

    #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;            
         sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,对于普通
   应用
必须设为 on,   sendfile()要比组合read()和write()以及打开关闭丢弃缓冲更加有效
   如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,以平衡磁盘与网络I/O处理速度,降低  系统的uptime
     注意:如果图片显示不正常把这个改成off。

    #tcp_nopush     on;                      防止网络阻塞, 在一个数据包里发送所有头文件,而不一个接一个的发送

    #keepalive_timeout  0;                  连接超时时间 
    keepalive_timeout  65;

    #gzip  on;                                  开启gzip压缩 



    server {                                            server模块指定一个或多个主机名
        listen       80;                               该虚拟主机监听端口, 可以为listen *:80、listen 127.0.0.1:80等形式
        server_name  localhost;         虚拟主机名, 域名可以有多个,用空格隔开

        #charset koi8-r;                         默认编码

        #access_log  logs/host.access.log  main;              设定本虚拟主机的访问日志

        location / {                                          location模块可以对特定文档进行约束
            root   html;                                     定义服务器的默认网站根目录位置
            index  index.html index.htm;    定义首页索引文件的名称
        }

        #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;
        }


         对php访问请求的处理
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80    通过apache处理
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 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
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


     其他访问虚拟主机的方法,通过ip、端口号
    # 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 ssl;
    #    server_name  localhost;

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

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

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

}



一些其他功能:

访问控制 allow/deny

Nginx 的访问控制模块默认就会安装,而且写法也非常简单,可以分别有多个allow,deny,允许或禁止某个ip或ip段访问,依次满足任何一个规则就停止往下匹配。如:

location /nginx-status {
  stub_status on;
  access_log off;
#  auth_basic   "NginxStatus";
#  auth_basic_user_file   /usr/local/nginx-1.6/htpasswd;

  allow 192.168.10.100;
  allow 172.29.73.0/24;
  deny all;
}
 
 
 
 

我们也常用 httpd-devel 工具的 htpasswd 来为访问的路径设置登录密码:

de># htpasswd -c htpasswd admin
New passwd:
Re-type new password:
Adding password for user admin

# htpasswd htpasswd admin    //修改admin密码
# htpasswd htpasswd sean    //多添加一个认证用户
de>

这样就生成了默认使用CRYPT加密的密码文件。打开上面nginx-status的两行注释,重启nginx生效。

 
 
 
 

列出目录 autoindex

Nginx默认是不允许列出整个目录的。如需此功能,打开nginx.conf文件,在location,server 或 http段中加入de>autoindex on;de>,另外两个参数最好也加上去:

  • de>autoindex_exact_size off;de> 默认为on,显示出文件的确切大小,单位是bytes。改为off后,显示出文件的大概大小,单位是kB或者MB或者GB
  • de>autoindex_localtime on;de> 默认为off,显示的文件时间为GMT时间。改为on后,显示的文件时间为文件的服务器时间
de>location /images {
  root   /var/www/nginx-default/images;
  autoindex on;
  autoindex_exact_size off;
  autoindex_localtime on;
  }
de>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值