Nginx安装与基本配置

导读:本篇文章讲解 Nginx安装与基本配置,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

Nginx安装

下载nginx

http://nginx.org/en/download.html 官网下载nginx tar包。
nginx版本:nginx-1.20.1

nginx相关依赖安装

安装nginx

  1. 解压nginx
tar -zxvf nginx-1.20.1.tar.gz
  1. 进入解压目录执行 ./configure
  2. 在解压目录执行 make && make install

防火墙配置

  1. 查看已经开放的防火墙端口
[root@localhost nginx-1.20.1]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens33
  sources:
  services: cockpit dhcpv6-client ssh
  ports: 27017/tcp 80/tcp 8121-8124/tcp 8848/tcp
  protocols:
  masquerade: no
  forward-ports:
  source-ports:
  icmp-blocks:
  rich rules:
  1. 如果80端口不在ports中,执行开放命令
[root@localhost nginx-1.20.1]# firewall-cmd --add-service=http --permanent
success
[root@localhost nginx-1.20.1]# firewall-cmd --add-port=80/tcp  --permanent
Warning: ALREADY_ENABLED: 80:tcp
success
[root@localhost nginx-1.20.1]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens33
  sources:
  services: cockpit dhcpv6-client http ssh
  ports: 27017/tcp 80/tcp 8121-8124/tcp 8848/tcp
  protocols:
  masquerade: no
  forward-ports:
  source-ports:
  icmp-blocks:
  rich rules:
# 重启防火墙
[root@localhost nginx-1.20.1]# firewall-cmd --reload
success

Nginx配置

nginx常用的命令

  1. 启动命令
    /usr/local/nginx/sbin/nginx
  2. 关闭命令
    /usr/local/nginx/sbin/nginx -s stop
  3. 重新加载命令
    /usr/local/nginx/sbin/nginx -s reload

nginx.conf 配置文件

  1. nginx安装目录下,其默认的配置文件都放在这个目录的conf目录下,主配置文件nginx.conf也在其中,后续对nginx的使用基本都是对此配置文件进行相应的修改。
[root@localhost conf]# ll
total 40
-rw-r--r--. 1 1001 1001 1077 May 25 20:35 fastcgi.conf
-rw-r--r--. 1 1001 1001 1007 May 25 20:35 fastcgi_params
-rw-r--r--. 1 1001 1001 2837 May 25 20:35 koi-utf
-rw-r--r--. 1 1001 1001 2223 May 25 20:35 koi-win
-rw-r--r--. 1 1001 1001 5231 May 25 20:35 mime.types
-rw-r--r--. 1 1001 1001 2656 May 25 20:35 nginx.conf
-rw-r--r--. 1 1001 1001  636 May 25 20:35 scgi_params
-rw-r--r--. 1 1001 1001  664 May 25 20:35 uwsgi_params
-rw-r--r--. 1 1001 1001 3610 May 25 20:35 win-utf

nginx.conf整体介绍

# 工作线程个数
worker_processes  1;
# events块
events {
    worker_connections  1024;
}
# http
http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }
}
  1. 第一部分:全局块
    从配置文件开始到events块之间的内容,主要会设置一些影响nginx服务器整体运行的配置指令,主要包括配置运行Nginx服务器的用户(组)、允许生成的worker process数,进程PID存放路径、日志存放路径和类型以及配置文件的引人等。
    比如上面第一行配置的:
worker_processes  1;

这是nginx服务器并发处理服务的关键配置,worker_processes值越大,可以支持的并发处理量也越多,但是会受到硬件、软件等设备的制约。

  1. 第二部分:events块
    比如上面的配置:
events {
	worker_connections  1024;
}

events块涉及到客户端连接Nginx服务器的网络连接,常用的设置包括是否开启多worker processes下的网络连接进行序列化,是否同时接收多个网络连接,选择哪种事件驱动模型来处理连接请求,每个worker processes可以同时处理的最大连接数等。
上述例子就表示每个work process支持的最大连接数为 1024。
这部分的配置对Nginx的性能影响较大,在实际中应该灵活配置。

  1. 第三部分:http块
http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }
}

这是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)进行匹配,对特定的请求进行处理。地址定向、数据缓存和应答控制等功能,还有许多第三方模块的配置也在这里进行。

测试

使用默认配置文件,启动nginx服务器进行测试。

# 启动
[root@localhost conf]# /usr/local/nginx/sbin/nginx -c /opt/nginx/nginx-1.20.1/conf/nginx.conf
# 访问nginx服务器
[root@localhost conf]# curl http://localhost/
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@localhost conf]#

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/100326.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!