美文网首页
nginx 基础知识

nginx 基础知识

作者: 海里的沙丁鱼 | 来源:发表于2018-01-15 18:12 被阅读0次

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

相关文章

  • 【转】Nginx入门指南,快速搭建静态文件服务器和代理服务器

    本文介绍 Nginx 入门基础知识,让你迅速搭建 Nginx 服务器。主要内容包括 Nginx 安装和简单使用、N...

  • Nginx学习

    1、Nginx基础知识 1.1认识Nginx 百科给出的解释: 总结: Nginx是一个高性能的Http和反向代理...

  • nginx负载均衡实验(全docker环境)

    Nginx 负载均衡基础知识nginx 的 upstream 目前支持 4 种方式的分配1)、轮询(默认)每个请...

  • Nginx负载均衡

    Nginx负载均衡一些基础知识: nginx 的 upstream目前支持 4 种方式的分配 1)、轮询(默认) ...

  • Nginx的安装部署

    第一、首先,需要知道的nginx的六个基础知识; 1.nginx的来源、作用和特点是什么? 答:nginx的来源:...

  • 集群搭建tomcat+keepalived+nginx+memc

    集群搭建tomcat+keepalived+nginx+memcached 一、基础知识 1、linux基本命令 ...

  • nginx基础知识

    nginx主配置文件 nginx.conf 配置文件结构如下: location匹配规则 语法规则location...

  • nginx 基础知识

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

  • Nginx基础知识

    简介: 概念:Nginx(engine X)是一个轻量级、高性能的HTTP和反向代理服务器。 特点:占有能存少,并...

  • Nginx基础知识

    本文主要介绍Nginx相关的基础技术,并做一些配置说明,具体实践,需要靠各位自己在实际中进行践行。 Nginx是什...

网友评论

      本文标题:nginx 基础知识

      本文链接:https://www.haomeiwen.com/subject/dipdoxtx.html