Nginx入门到调优(二)

一、Nginx配置文件详解

Nginx配置文件在nginx安装目录下的conf文件下,文件名称为nginx.conf,默认的内容如下:

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

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

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

}

根据上述文件,我们可以明显的将nginx.conf配置文件分为三部分:

  1. 第一部分:全局块
      从配置文件到 events 块之间的内容,主要会设置一些影响 Nginx 服务器整体运行的配置指令,主要包括配置运行 Nginx 服务器的用户(组)、允许生成的 worker process 数,进程 PID 存放路径、日志存放路径和类型以及配置文件的引入等。
      比如上面的第一行配置:
    这是 Nginx 服务器并发处理服务的关键配置,worker_processes 值越大,可以支持的并发处理量也越多,但是会受到硬件、软件等设备的制约。
worker_processes  1;
  1. 第二部分:events 块
      events 块涉及的指令主要影响 Nginx 服务器与用户的网络连接,常用的设置包括是否开启对多 work process 下的网络连接进行序列化,是否允许同事接收多个网络连接,选取哪种事件驱动类型来处理连接请求,每个 work process 可以同事支持的最大连接数等。
      上述的例子就表示每个 work process 支持的最大连接数为1024。
      这部分的配置对 Nginx 的性能影响较大,在实际中应该灵活配置。
  2. 第三部分:http块
      这是Nginx服务器配置中最频繁的部分,代理、缓存和日志定义等绝大部分的功能和第三方模块的配置都在这里。需要注意的是:http块也可以包括http全局块、server块。
    ① http 全局块
      http 全局块配置的指令包括文件的引入、MIME-TYPE 定义、日志自定义、连接超时时间、单链接请求数上限等。
    ② server 块
      这块和虚拟主机有密切关系,虚拟主机从用户角度看,和一台独立的硬件主机是完全一样的,该技术的产生是为了节省互联网服务器硬件成本。
      每个 http 块可以包括多个 server 块,而每个 server 块就相当于一个虚拟主机。
      每个 server 块也分为全局 server 块,以及可以同时包含多个 location 块。
  • 全局 server 块
    最常见的配置是本虚拟机主机的监听配置和本虚拟机主机的名称或 IP 配置。
  • location 块
    一个 server 块可以配置多个 location 块。
    这块的主要作用是基于 Nginx 服务器接收到请求字符串(例如:server_name/uri-string),对虚拟主机名称(也可以是 IP 别名)之外的字符串(例如:前面的 /uri-string)进行匹配,对特定的请求进行处理。地址定向、数据缓存和应答控制等功能,还有许多第三方模块的配置也在这里进行。
      该指令用于匹配 URL。语法如下:
    location [ = | ~ | ~* | ^~] uri {
    
    }
    
    • = :用于不含正则表达式的 uri 前,要求请求字符串与 uri 严格匹配,如果匹配成功,就停止继续向下搜索并立即处理该请求。
    • ~ :用于表示 uri 包含正则表达式,并且区分大小写。
    • ~* :用于表示 uri 包含正则表达式,并且不区分大小写。
    • ^~ :用于表示不含正则表达式的 uri 前,要求 Nginx 服务器找到标识 uri 和请求字符串匹配度最高的 location 后,立即使用此 location 处理请求,而不再使用 location 块中的正则 uri 和请求字符串做匹配。
      注意:如果 uri包含正则表达式,则必须要有 ~ 或者 ~* 标识

二、Nginx配置实例-反向代理

1 反向代理实例一

  1. 实现效果:
      打开浏览器,在浏览器地址栏输入地址 www.123.com,跳转到 Linux 系统 tomcat 主页面中。
  2. 准备工作:
  • 在 Linux 系统中安装 tomcat ,使用默认端口 8080;
    • tomcat 安装文件放到 Linux 系统中,解压
    • 进入 tomcat 的 bin 目录中, ./startup.sh 启动 tomcat 服务器
  • 对外开放访问的端口;
    ## 防火墙开放8080端口
    [root@localhost bin]# firewall-cmd --add-port=8080/tcp --permanent
    ## 重启防火墙
    [root@localhost bin]# firewall-cmd --reload
    
  • 在 Windows 系统中通过浏览器访问 tomcat 服务器;
  1. 配置反向代理
  • 第一步:编辑 Windows 机器的 hosts 文件
    文件目录在 C:\Windows\System32\drivers\etc 位置
  • 第二步:在 Nginx 进行请求转发的配置(反向代理配置)
    server {
          listen       80;
          server_name  192.168.xx.129;
    
          #charset koi8-r;
    
          #access_log  logs/host.access.log  main;
    
          location / {
              root   html;
              roxy_pass   http://127.0.0.1:8080;
              index  index.html index.htm;
          }
    
    具体流程如下图所示:
    在这里插入图片描述

2 反向代理实例二

  1. 实现效果:
      使用Nginx反向代理,根据访问的路径跳转到不同端口的服务中;
  • nginx监听端口为9001,
  • 访问 http://127.0.0.1:9001/edu/ 直接跳转号 127.0.0.0.1:8081
  • 访问 http://127.0.0.1:9001/vod/ 直接跳转号 127.0.0.0.1:8082
  1. 准备工作:
  • 准备两个 Tomcat 服务器,一个8080端口,一个8081端口
  • 创建文件夹和测试页面
  1. 具体配置:
  • 第一步:修改 Nginx 的配置文件,进行反向代理配置
    server {
          listen       9001;
          server_name  192.168.xx.129;
    
          #charset koi8-r;
    
          #access_log  logs/host.access.log  main;
    
          location ~ /edu/ {
              roxy_pass   http://127.0.0.1:8080;
          }
    
          location ~ /vod/ {
              roxy_pass   http://127.0.0.1:8081;
          }
    
  • 第二步:对外开放访问的端口;
    ## 防火墙开放9001端口
    [root@localhost bin]# firewall-cmd --add-port=9001/tcp --permanent
      ## 防火墙开放8080端口
    [root@localhost bin]# firewall-cmd --add-port=8080/tcp --permanent
      ## 防火墙开放8081端口
    [root@localhost bin]# firewall-cmd --add-port=8081/tcp --permanent
    ## 重启防火墙
    [root@localhost bin]# firewall-cmd --reload
    
  • 第三步:测试
      浏览器分别访问 http://192.168.xx.129:9001/edu 和 http://192.168.xx.129:9001/vod,查看显示的页面。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值