ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

Nginx简述

2021-09-10 21:01:28  阅读:142  来源: 互联网

标签:http 配置文件 nginx server Nginx 简述 html


1 Nginx 

Nginx("engine x") 是一个高性能的HTTP和反向代理服务器,特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好.

2.正向代理和反向代理

正向代理类似一个跳板机,代理访问外部资源. 
用户-->代理服务器-->要访问的网站

反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,
并将从服务器上得到的结果返回给internet上请求连接的客户端,此时代理服务器对外就表现为一个反向代理服务器;
用户  <----  代理服务器<-------网站1,网站2,网站3  

3.Nginx安装

3.1 Nginx相关依赖

sudo yum -y install openssl openssl-devel pcre pcre-devel zlib zlib-devel gcc gcc-c++

3.2 安装

解压之后 进入解压缩目录

./configure   --prefix=/opt/module/nginx
make && make install

3.3 启动 Nginx

先执行
sudo setcap cap_net_bind_service=+eip /opt/module/nginx/sbin/nginx
Nginx默认的端口是80  默认情况下非root用户不允许使用1024以下端口
执行命令 让当前用户的某个应用也可以使用1024以下的端口 

在/opt/module/nginx/sbin目录下执行  ./nginx
<1>查看执行情况
ps -ef |grep nginx

<2>在浏览器中输入
http://Ava02/ 出现欢迎界面

表示启动成功

3.4 其他命令

重启Nginx
./nginx  -s reload

关闭Nginx
./nginx  -s  stop

配置检查
当修改Nginx配置文件后,可以使用Nginx命令进行配置文件语法检查,用于检查Nginx配置文件是否正确
/opt/module /nginx/sbin/nginx -c /opt/module/nginx/conf/nginx.conf -t


通过配置文件启动
./nginx -c /opt/module/nginx/conf/nginx.conf
/opt/module/nginx/sbin/nginx -c /opt/module/nginx/conf/nginx.conf
其中-c是指定配置文件,而且配置文件路径必须指定绝对路径

 

配置检查
当修改Nginx配置文件后,可以使用Nginx命令进行配置文件语法检查,用于检查Nginx配置文件是否正确
/opt/module /nginx/sbin/nginx -c /opt/module/nginx/conf/nginx.conf -t

4.nginx配置文件

nginx.conf 文件结构

...              #全局块

events {         #events块
   ...
}

http      #http块
{
    ...   #http全局块
    server        #server块
    { 
        ...       #server全局块
        location [PATTERN]   #location块
        {
            ...
        }
        location [PATTERN] 
        {
            ...
        }
    }
    server
    {
      ...
    }
    ...     #http全局块
}


 1、全局块:配置影响nginx全局的指令。一般有运行nginx服务器的用户组,nginx进程pid存放路径,日志存放路径,配置文件引入,允许生成worker process数等。
 2、events块:配置影响nginx服务器或与用户的网络连接。有每个进程的最大连接数,选取哪种事件驱动模型处理连接请求,是否允许同时接受多个网路连接,开启多个网络连接序列化等。
 3、http块:可以嵌套多个server,配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置。如文件引入,mime-type定义,日志自定义,是否使用sendfile传输文件,连接超时时间,单连接请求数等。
 4、server块:配置虚拟主机的相关参数,一个http中可以有多个server。
 5、location块:配置请求的路由,以及各种页面的处理情况。

4.2 配置文件注释

#配置worker进程运行用户 nobody也是一个linux用户 一般用于启动程序,没有密码 
#user  nobody;
#配置工作进程数目,根据硬件调整,通常等于CPU数量或者2倍于CPU的数量
worker_processes  1;
#配置日志路径,级别 级别为:debug|info|notice|warn|error|crit|alert|emerg 默认是error
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid; #指定nginx进程运行文件存放地址


events {
    worker_connections  1024; #每个worker最大连接数,默认为1024
    #nginx支持的总连接数就等于 worker_connections *  worker_processes
}

#配置http服务器,利用它的反向代理功能提供负载均衡支持
http {
    #配置nginx支持哪些多媒体类型,可以在conf/mime.types查看支持哪些多媒体类型
    include       mime.types;
    #默认文件类型流类型,可以理解为支持任意类型
    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日志及存放路径,并使用上面定义的main日志格式
    #access_log  logs/access.log  main;

    sendfile        on; #开启高效文件传输模式
    #tcp_nopush     on; #防止网络阻塞

    #keepalive_timeout  0;
    keepalive_timeout  65; #长连接超时时间,单位是秒

    #gzip  on;  #开启gzip压缩输出
  
  #配置虚拟主机 可以配置多个块
    server {
        listen       80; #配置监听端口
        server_name  localhost; #监听地址

        #charset koi8-r; #配置字符集

        #access_log  logs/host.access.log  main; #配置本虚拟主机的访问日志
        #默认的匹配斜杠/的请求,当访问路径中有斜杠/,会被该location匹配到并进行处理
        location / {
            #root是配置服务器的默认网站根目录位置,默认为nginx安装主目录下的html目录
            root   html;
            #配置首页文件的名称
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #配置50x错误页面
        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
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #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;
        #}
    }


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

}

 


        

 

标签:http,配置文件,nginx,server,Nginx,简述,html
来源: https://www.cnblogs.com/zzz01/p/15253003.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有