nginx 基础知识

nginx由于自身出色的性能,在世界范围内是受到了越来越多人的关注。国内使用nginx网站的用户也很多:百度、京东、新浪、网易、腾讯、淘宝等。

一 那么Nginx到底是什么?有什么优点呢?

Nginx(‘engine x’)是一款轻量级的WEB服务器/反向代理服务器和电子邮件代理服务器。特点是占有内存少、并发能力强、移植性强。

二 下面介绍在ubuntu14.04下安装nginx后的目录介绍:

1.先给出一个nginx目录下的所有文件:


root@iZ28:~# cd /etc/nginx/
root@iZ28:/etc/nginx# ls
conf.d          mime.types           nginx.conf       sites-enabled
fastcgi_params  naxsi_core.rules     proxy_params     uwsgi_params
koi-utf         naxsi.rules          scgi_params      win-utf
koi-win         naxsi-ui.conf.1.4.1  sites-available
root@iZ28:/etc/nginx# vi nginx.conf 


2.查看nginx.conf

user www-data;                
worker_processes 4;             
pid /run/nginx.pid;

events {
    worker_connections 768;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # nginx-naxsi config
    ##
    # Uncomment it if you installed nginx-naxsi
    ##

    #include /etc/nginx/naxsi_core.rules;

    ##
    # nginx-passenger config
    ##
    # Uncomment it if you installed nginx-passenger
    ##
    
    #passenger_root /usr;
    #passenger_ruby /usr/bin/ruby;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}


#mail {
#   # See sample authentication script at:
#   # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
# 
#   # auth_http localhost/auth.php;
#   # pop3_capabilities "TOP" "USER";
#   # imap_capabilities "IMAP4rev1" "UIDPLUS";
# 
#   server {
#       listen     localhost:110;
#       protocol   pop3;
#       proxy      on;
#   }
# 
#   server {
#       listen     localhost:143;
#       protocol   imap;
#       proxy      on;
#   }
#}
                

上面是我nginx最初的配置信息

3.添加注释

1.worker_processes:指明了nginx要开启的进程数,据官方说法,一般开一个就够了,多开几个,可以减少机器io带来的影响。 一般为当前机器总cpu核心数的1到2倍,可与worker_cpu_affinity连用指定CPU。`

2.error_log:异常日志输出目录

3.worker_connections:指定并发连接数,一般与worker_rlimit_nofile连用

4.worker_rlimit_nofile:指定同时处理的文件限制

5.http: 详细网站配置

6.http[include] :指包含文件,可以把配置文件存储在其他位置,如把网站每个server以单独文件形式存放在/etc/nginx/conf.d/下。

7.http[gzip]:gzip压缩相关配置,是否开启压缩,压缩内容,压缩的等级等等

8.http[server]:网站服务的各项配置,包含监听,主机头,文件路径,网站目录,是否代理,重定向等等配置

9.http[upstream]:负载均衡各项配置,服务器地址,权重,启停、备用等等配置

三 普通网站配置示列:

nginx/site-available里有一个默认的default文件。基本配置如下:
(每次配置新的域名时,只需要在该目录下直接复制一份default进行修改->外链->重启nginx就可以了。具体教程看这

1.http网站示例:

server {
    listen       80;
    server_name  localhost;
    
     location / {
        root   /usr/share/nginx/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   /usr/share/nginx/html;
    }

 }

2.https网站示列:

server {
    listen      443 ssl;
    server_name  ***.com;

    ssl_certificate /etc/nginx/server.crt;
    ssl_certificate_key /etc/nginx/server.key;


    location / {
        root   /usr/share/nginx/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   /usr/share/nginx/html;
    }

}

3.注释:

1).listen: 监听端口,加ssl 为https通信方式。

2).server_name:网站服务名,主机头

3).error_page:错误页

4).location: 虚拟位置,如 “/” 根目录,如“/images/”,“/50x.htm"等等

5).root:文件实际位置

6).index:起始页

7).ssl_certificate:ssl证书存放位置(由证书颁发机构提供)

8).ssl_certificate_key:ssl证书私钥存放位置(由证书颁发机构提供)

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

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、每个指令必须有分号结束。

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 第一章 Nginx简介 Nginx是什么 没有听过Nginx?那么一定听过它的“同行”Apache吧!Ngi...
    JokerW阅读 32,968评论 24 1,002
  • Page 1:nginx 服务器安装及配置文件详解 CentOS 6.2 x86_64 安装 nginx 1.1 ...
    xiaojianxu阅读 8,655评论 1 41
  • Nginx简介 解决基于进程模型产生的C10K问题,请求时即使无状态连接如web服务都无法达到并发响应量级一万的现...
    魏镇坪阅读 2,180评论 0 9
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,322评论 19 139
  • 1.简介:  Nginx:engine X ,2002年,开源,商业版 http协议:web服务器(类似于ht...
    尛尛大尹阅读 1,985评论 0 3

友情链接更多精彩内容