nginx的安装配置详解

title: nginx的安装配置详解
tags: nginx,虚拟服务器,curl


nginx的安装配置详解

1. 介绍各个常用的服务端口

  1. 21 ftp ;22 ssh;25 smtp;3306 mysql;873 rsync;3389 远程桌面;161 snmp;111 rpcbind;80 www http;443 https;110 pop3;53 dns;514 rsyslog
  2. 我们常用的nslookup和dig查询域名解析工具的安装包为bind-utils,如yum install bind-utils -y
  3. 我们通常配好了nginx后,需要测试一下,一般通过curl命令,curl -I www.baidu.com 可以反馈百度的head信息,可以直接yum install curl -y进行安装。
  4. 如果安装源码包,有一些编译工具没有安装的话,我们可以安装开发包组Development tools,如yum groupinstall "Development tools" 方式进行安装。

2. nginx的优势

  1. 配置简单,灵活,轻便
  2. 高并发(静态小文件),静态几万并发
  3. 占资源小,2W并发,开10个线程服务,内存只消耗几百M
  4. 功能种类比较多(web,cache,proxy),但是每个功能都不是特别强
  5. nginx 可以配合动态服务(FASTCGI接口)
  6. 利用nginx可以对ip限速,可以限制连接数
  7. nginx的并发很高,1-2M的小文件,可以支持同时1-3W的并发,但是nginx只能处理静态资源,如果是动态资源就要借助php和数据库了,但是后两者并发都比较低
    只有同时300-800的并发,因此nginx如果处理动态资源,主要是看后两者的并发,这取决于木桶原理,极端点取决于短板php和DB。

3. nginx的安装

  1. 必须先安装pcre,因为nginx有rewrite功能,rewrite模块需要pcre的支持,经验提示,如果安装什么提示缺少什么库,一般都是缺devel,只要在前面加个-devel即可

    [[email protected] conf]# rpm -qa pcre
    pcre-7.8-7.el6.x86_64
    [[email protected] conf]# rpm -qa pcre-devel
    pcre-devel-7.8-7.el6.x86_64
  2. 安装openssl -devel,因为nginx有安全模块,因此需要安装openssl 和openssl-devel
    [[email protected] conf]# rpm -qa openssl
    openssl-1.0.1e-57.el6.x86_64
    [[email protected] conf]# rpm -qa openssl-devel
    openssl-devel-1.0.1e-57.el6.x86_64
    [[email protected] conf]# 
  3. 安装nginx,先下载源码包,然后解压缩,然后通过./configure配置,然后编译和安装。其中./configure --help
    可以查看到一些配置的参数,如不需要的参数用--without-mail_smtp_module取消,./configure只是配置文件,并不是安装,只是生成了一个makefile。
    这里安装只有3个组件,其他必须的都会默认安装,-user=nginx指定用户,--group=nginx指定组,--with-http_ssl_module激活sl加密模块,--with-http_stub_status_module激活状态信息
    wget http://nginx.org/download/nginx-1.6.3.tar.gz
    tar xf nginx-1.6.3.tar.gz
    cd nginx-1.6.3
    ./configure --prefix=/application/nginx-1.6.3 --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module
    echo $?
    make && make install
    useradd nginx -s /sbin/nologin -M
    id nginx
    grep nginx /etc/group
    ln -s /application/nginx-1.6.3/ /application/nginx
  4. 注意,有很多人提示没有安装gcc,或者make,我们安装的时候最好把开发库工具安装上去
    yum groupinstall "Development tools" 如果是源码安装pcre的话,安装nginx可能会找不到pcre,而提示无法找到pcre,
    因此编译的时候,我们必须用--with=/usr/local/bin/pcre 指定pcre的位置。
  5. 安装完成,启动服务: /application/nginx/sbin/nginx,检查一下,80端口是否打开,进程是否存在。
    [[email protected] ~]# /application/nginx/sbin/nginx
    [[email protected] ~]# lsof -i :80
    COMMAND  PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
    nginx   1382  root    6u  IPv4  13412      0t0  TCP *:http (LISTEN)
    nginx   1383 nginx    6u  IPv4  13412      0t0  TCP *:http (LISTEN)
    [[email protected] ~]# ps -ef |grep nginx |grep -v grep
    root       1382      1  0 23:24 ?        00:00:00 nginx: master process /application/nginx/sbin/nginx
    nginx      1383   1382  0 23:24 ?        00:00:00 nginx: worker process
    [[email protected] ~]#
    [[email protected] ~]# ss -lntup |grep nginx
    tcp    LISTEN     0      128                    *:80                    *:*      users:(("nginx",1382,6),("nginx",1383,6))
  6. 在客户端浏览器输入 192.168.50.2:80,看能不能打开nginx的初使页面。

4. nginx服务的两个相关命令

  1. 改完nginx配置文件后,我们不能着急重启nginx服务,必须先检查一下配置文件的语法,如:

    [[email protected] sbin]# /application/nginx/sbin/nginx -t
    nginx: the configuration file /application/nginx-1.6.3/conf/nginx.conf syntax is ok
    nginx: configuration file /application/nginx-1.6.3/conf/nginx.conf test is successful
  2. 改完nginx配置文件后,我们必须要重启nginx才能让配置生效,重启的时候要平滑重启。
    [[email protected] sbin]# /application/nginx/sbin/nginx -s reload
    [[email protected] sbin]# 
  3. 查看nginx安装配置的参数,/application/nginx/sbin/nginx -V,如:
    [[email protected] sbin]# /application/nginx/sbin/nginx -V
    nginx version: nginx/1.6.3
    built by gcc 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC)
    TLS SNI support enabled
    configure arguments: --prefix=/application/nginx-1.6.3 --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module
    [[email protected] sbin]# 

5. 安装目录文件详解

  1. nginx安装完成后文件详解如:

    [[email protected] nginx]# ls |grep -v temp |xargs tree -L 1
    conf 配置文件
    |--nginx.conf
    html 站点目录
    |-- 50x.html
    `-- index.html
    logs  日志信息
    |-- access.log
    |-- error.log
    `-- nginx.pid
    sbin  执行文件
    `-- nginx
  2. nginx的配置文件nginx.conf,我们配置的时候可以先把原来的备份,然后借用nginx.conf.default过来直接配置,配置文件一定要注意{},必须成对出现,不能拆散如:
    cat 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;
    这几行属于main区,Nginx核心功能模块
    events {
     worker_connections  1024;     一个worker所能处理的多少的并发连接数
    14 }
    这几行属于event区,Nginx核心功能模块
    http {
     include       mime.types;
     default_type  application/octet-stream;
    这个属于http区开始,Nginx核心功能模块
    ##################################################
    实例搭建两个虚拟主机www.maiyat.com  bbs.maiyat.com
    ##################################################
    cp nginx.conf nginx.conf.bak
    egrep -v "#|^$" nginx.conf.default > nginx.conf
    worker_processes  1;     worker进程的数量
    events {                 事件区块的开始
    worker_connections  1024; 每个woker进程支持的最大连接数
    }  事件区块结束
    http {             http区块开始
    include       mime.types;  Nginx支持的媒体类型库文件包含
    default_type  application/octet-stream;  默认的媒体类型
    sendfile        on;                  开启高效传输模式
    keepalive_timeout  65;                连接超时
    server {                 第一个server区块开始,代表一个独立的虚拟主机站点
        listen       80;        提供服务的端口,默认是80
        server_name  www.maiyat.com;  提供服务的域名主机名
        location / {                      第一个Location区块的开始
            root   html/www;              站点的根目录,相对路径,相对于Nginx的安装路径
            index  index.html index.htm;   默认的首页文件,多个用空格分开
        }                                    第一个区块结束
    }
        server {
        listen       80;
        server_name  bbs.maiyat.com;
        location / {
            root   html/bbs;
            index  index.html index.htm;
        }
                error_page   500 502 503 504  /50x.html;         出现对应的http状态码使用50x.html回应
        location = /50x.html {                        Location区块的开始,访问50x.html
            root   html;                    指定对应的站点目录为html
            }
    }
    }                 http区块结束
    
  3. 测试www.maiyat.com 及bbs.maiyat.com有没有搭建好
    3.1 先在/etc/hosts解析好,可以一个ip后面跟几个不同的域名,如
    [[email protected] conf]# cat /etc/hosts
    #127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
    ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
    127.0.0.1       maiyat  www.maiyat.com  bbs.maiyat.com  blog.maiyat.com
    192.168.50.2    maiyat  www.maiyat.com  bbs.maiyat.com  blog.maiyat.com

    3.2 如果是windows的hosts文件在 C:\Windows\System32\drivers\etc
    3.3 linux下用curl命令看能不能返回消息,windows下用浏览器输入域名

    [[email protected] conf]# curl www.maiyat.com
    hello world
    [[email protected] conf]# curl bbs.maiyat.com
    hello oldboy
    [[email protected] conf]# curl blog.maiyat.com
    happy comeon maiyat.com !

6. 虚拟主机还可以使用端口和IP进行配置

  1. 上述讲的是最常用的通过域名来配置虚拟主机,但是虚拟主机还可以通过端口和ip进行配置。如基于端口的配置,直接在配置文件的listen 80,80改为8001或者其他的就好了,其他的都不用改,注意要把几个虚拟主机的域名统一,需要重启nginx服务。
[[email protected] conf]# vi nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       8001;
        server_name  www.maiyat.com;
        location / {
            root   html/www;
            index  index.html index.htm;
        }
}
        server {
        listen       8002;
        server_name  www.maiyat.com;
        location / {
            root   html/bbs;
            index  index.html index.htm;
        }
    }
server {
        listen       8003;
        server_name  www.maiyat.com;
        location / {
            root   html/blog;
            index  index.html index.htm;
        }
    }
}
[[email protected] conf]# cd ../sbin/
[[email protected] sbin]# ./nginx -t
nginx: the configuration file /application/nginx-1.6.3/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.6.3/conf/nginx.conf test is successful
[[email protected] sbin]# ./nginx -s reload
[[email protected] sbin]# lsof -i :8001
COMMAND  PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   1382  root   10u  IPv4  13964      0t0  TCP *:vcom-tunnel (LISTEN)
nginx   1476 nginx   10u  IPv4  13964      0t0  TCP *:vcom-tunnel (LISTEN)
[[email protected] sbin]# lsof -i :8002
COMMAND  PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   1382  root   11u  IPv4  13965      0t0  TCP *:teradataordbms (LISTEN)
nginx   1476 nginx   11u  IPv4  13965      0t0  TCP *:teradataordbms (LISTEN)
[[email protected] sbin]# lsof -i :8003
COMMAND  PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   1382  root   12u  IPv4  13966      0t0  TCP *:mcreport (LISTEN)
nginx   1476 nginx   12u  IPv4  13966      0t0  TCP *:mcreport (LISTEN)
[[email protected] sbin]# curl www.maiyat.com:8001
hello world
[[email protected] sbin]# curl www.maiyat.com:8002
hello oldboy
[[email protected] sbin]# curl www.maiyat.com:8003
happy comeon maiyat.com !

2 基于ip进行配置虚拟主机,注意修改的时候把端口恢复为80。

[[email protected] sbin]# cd ../conf
[[email protected] conf]# vi nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       192.168.50.250:80;
        server_name  www.maiyat.com;
        location / {
            root   html/www;
            index  index.html index.htm;
        }
}
        server {
        listen       192.168.50.251:80;
        server_name  www.maiyat.com;
        location / {
            root   html/bbs;
            index  index.html index.htm;
        }
    }
server {
        listen       192.168.50.253:80;
        server_name  www.maiyat.com;
        location / {
            root   html/blog;
            index  index.html index.htm;
        }
    }
}
[[email protected] conf]#
[[email protected] conf]# cd ../sbin/
[[email protected] sbin]# ./nginx -t
nginx: the configuration file /application/nginx-1.6.3/conf/nginx.conf syntax is ok
nginx: [emerg] bind() to 192.168.50.250:80 failed (99: Cannot assign requested address)
nginx: configuration file /application/nginx-1.6.3/conf/nginx.conf test failed
[[email protected] sbin]# ip addr add 192.168.50.250/24 dev eth0
[[email protected] sbin]# ip addr add 192.168.50.251/24 dev eth0
[[email protected] sbin]# ip addr add 192.168.50.253/24 dev eth0
[[email protected] sbin]# ./nginx -t
nginx: the configuration file /application/nginx-1.6.3/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.6.3/conf/nginx.conf test is successful
[[email protected] sbin]# ./nginx -s reload
[[email protected] sbin]# curl 192.168.50.250
hello world
[[email protected] sbin]# curl 192.168.50.251
hello oldboy
[[email protected] sbin]# curl 192.168.50.253
happy comeon maiyat.com

原文地址:http://blog.51cto.com/ouyangtao/2137227

时间: 2024-08-05 11:14:09

nginx的安装配置详解的相关文章

使用LVS实现负载均衡原理及安装配置详解

转:http://www.cnblogs.com/liwei0526vip/p/6370103.html 使用LVS实现负载均衡原理及安装配置详解 负载均衡集群是 load balance 集群的简写,翻译成中文就是负载均衡集群.常用的负载均衡开源软件有nginx.lvs.haproxy,商业的硬件负载均衡设备F5.Netscale.这里主要是学习 LVS 并对其进行了详细的总结记录. 一.负载均衡LVS基本介绍 LB集群的架构和原理很简单,就是当用户的请求过来时,会直接分发到Director

使用 LVS 实现负载均衡原理及安装配置详解

使用 LVS 实现负载均衡原理及安装配置详解 来源:肖邦linux 发布时间:2017-02-19 阅读次数:106 0 负载均衡集群是 load balance 集群的简写,翻译成中文就是负载均衡集群.常用的负载均衡开源软件有nginx.lvs.haproxy,商业的硬件负载均衡设备F5.Netscale.这里主要是学习 LVS 并对其进行了详细的总结记录. 一.负载均衡LVS基本介绍 LB集群的架构和原理很简单,就是当用户的请求过来时,会直接分发到Director Server上,然后它把用

nginx与fastdfs配置详解与坑

nginx与fastdfs配置详解与坑 环境ubantu19.04fastdfs-5.11fastdfs-nginx-module-1.20libfastcommon-1.0.39nginx-1.15.12pcre2-10.33openssl-1.0.2r nginx配置过程 https://blog.csdn.net/tjcyjd/article/details/69663348 ssl与依赖库 https://blog.csdn.net/g1531997389/article/details

nginx一些参数配置详解

nginx的配置:    正常运行的必备配置:       1.user username [groupname];           指定运行worker进程的用户和组       2.pid /path/to/pidfile_name nginx的pid文件 3.worker_rlimit_nofile #;            一个worker进程所能够打开的最大文件句柄数:       4.worker_rlimit_sigpending #;            设定每个用户能够

Activiti(一)--安装配置详解

有一段时间没有更新文章了,虽然有一直在写文章,可是一直没有更新到博客内,这段时间写的文章大多还是以技术为主.接下来的系列文章将会来讨论企业工作流的开发,主要是来研究开源工作流Activiti的使用. 该篇文章来详细了解下Activiti 5.15的安装配置全过程,其实安装的过程相当的简单,因为随着Activiti的版本升级安装就变得简单了很多,但是它的配置方法没有想象的那么简单,在配置时需要注意很多问题,这里就来详细的了解下Activiti 5.15的配置全过程. Note:文章都是读者经过实践

NFS服务器原理和安装配置详解附案例演练

NFS服务器原理和安装配置详解附案例演练 1.什么是NFS服务器 NFS就是Network File System的缩写,它最大的功能就是可以通过网络,让不同的机器.不同的操作系统可以共享彼此的文件. NFS服务器可以让PC将网络中的NFS服务器共享的目录挂载到本地端的文件系统中,而在本地端的系统中来看,那个远程主机的目录就好像是自己的一个磁盘分区一样,在使用上相当便利: 2.NFS挂载原理 NFS服务器的挂载结构图: 如上图示: 当我们在NFS服务器设置好一个共享目录/home/public后

Windows Nano Server安装配置详解05:在虚拟机中部署NanoServer

1.将 NanoServerImageGenerator 文件夹从 Windows Server 2016 ISO 中 \NanoServer 文件夹复制到你硬盘上的文件夹. 拷贝到C盘根目录. 2.以管理员身份启动 Windows PowerShell,将目录更改为 NanoServerImageGenerator 文件夹所在的文件夹,然后导入模块,其方法为 Import-Module .\NanoServerImageGenerator -Verbose 3.通过运行以下命令(将提示你输入新

samba 4.7.16 安装配置详解

系统:Centos 7.4 x64位 服务版本:samba-4.7.1.samba-client-4.7 Samba 简介 Samba 是在Linux和UNIX系统上实现SMB协议的一个免费软件,由服务器及客户端程序构成. Samba最大的功能就是可以用于Linux与windows系统直接的文件共享和打印共享,Samba既可以用于windows与Linux之间的文件共享,也可以用于Linux与Linux之间的资源共享. Samba由两个主要程序组成,它们是smbd和nmbd.这两个守护进程在服务

Nginx虚拟主机配置详解

Nginx虚拟主机配置详解 一.虚拟主机介绍 虚拟主机使用的是特殊的软硬件技术,它把一台运行在因特网上的服务器主机分成一台台"虚拟"的主机,每台虚拟主机都可以是一个独立的网站,可以具有独立的域名,具有完整的Intemet服务器功能(WWW.FTP.Email等),同一台主机上的虚拟主机之间是完全独立的.从网站访问者来看,每一台虚拟主机和一台独立的主机完全一样. 利用虚拟主机,不用为每个要运行的网站提供一台单独的Nginx服务器或单独运行一组Nginx进程.虚拟主机提供了在同一台服务器.