Nginx的部署和虚拟主机配置

一、nginx简介

  • 一款高性能、轻量级Web服务软件, 稳定性高,系统资源消耗低
  • 对HTTP并发连接的处理能力高(能够处理高并发),单台物理服务器可支持30000~50000个并发请求

二、编译安装nginx

[root@localhost ~]# yum -y install pcre-devel zlib-devel
[root@localhost ~]# tar zxf nginx-1.12.2.tar.gz
[root@localhost ~]# cd nginx-1.12.2/
[root@localhost nginx-1.12.2]# ./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-http_stub_status_module ###统计状态模块
[root@localhost nginx-1.12.2]# make && make install
[root@localhost ~]# useradd -M -s /sbin/nologin nginx  ###创建一个不可登录的程序用户
[root@localhost ~]# ln -s /usr/local/nginx/sbin/nginx /usr/bin ###优化执行路径
[root@localhost ~]# nginx ###启动服务
[root@localhost ~]# netstat -anpt | grep nginx ###查看nginx服务是否开启
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      16429/nginx: master
[root@localhost ~]# killall -s QUIT nginx ###选项-s QUIT等于-3 停止服务
[root@localhost ~]# netstat -anpt | grep nginx
[root@localhost ~]# killall -s HUP nginx ###选项-s HUP等于-1 重新加载
[root@localhost ~]# netstat -anpt | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      16488/nginx: master
[root@localhost ~]# vi /etc/init.d/nginx ###制作管理脚本
#!/bin/bash
#chkconfig: 35 20 80
#description: nginx server
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
    start)
       $PROG
       ;;
    stop)
       killall -3 $PROG
       ;;
    restart)
       $0 stop
       $0 start
       ;;
    reload)
       killall -1 $PROG
       ;;
    *)
       echo "Usage: $0 {start|stop|reload|status}"
       exit 1
esac
exit 0
[root@localhost ~]# chmod +x /etc/init.d/nginx
[root@localhost ~]# chkconfig --add nginx
[root@localhost ~]# systemctl start nginx

三、主配置文件分析

全局配置

#user nobody;           ##指定用户,默认是匿名用户
worker_processes 1;   ##工作进程,实现高并发可以增加值
#error_log logs/error.log;  ##错误日志文件
#pid     logs/nginx.pid;      ##pid文件

I/O事件配置

events {           ##一个进程包含多个线程
    use epoll; #能显著提高程序在大量并发连接中只有少量活跃的情况下的系统CPU利用率
    worker_connections 4096;
}

http配置

http{}:协议层面;server{}:服务层面;location{}:网页站点目录层面
http {
     access_log logs/access.log main;
     sendfile     on;        ##发送邮件
     keepalive timeout 65;  ##连接超时时间
     server {
         listen  80;
         server_name localhost;    ##域名
         charset utf-8;       ##字符集
         location  / {
               root  html;     ##网页站点目录名称
               index index.html index.php; }    ##主页的相对路径
         error_page  500 502 503 504 /50x.html;    ##提示错误页面(500是服务器出错)
         location = /50x.html {  
            root  html; }                
    }
}

四、Nginx的访问状态统计

[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
user  nginx nginx ###修改#user  nobody为user  nginx nginx
error_log  logs/error.log  info ###去除#号
events {
        use epoll; ###新增此行,默认使用select/poll
    worker_connections  1024; ###表示一个工作进程允许1024个连接
}
 location ~ /status {  ###配置统计功能
             stub_status  on;
             access_log  off;
             } ###在server模块里的error_page上面增加
[root@localhost ~]# nginx -t ###检查一下配置文件
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost ~]# nginx -V ###查看版本号及开启的模块
nginx version: nginx/1.12.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC)
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module 
[root@localhost ~]# systemctl start nginx ###开启nginx服务
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0 ###关闭防火墙和核心防护

验证,访问192.168.247.160/status
在这里插入图片描述

nginx status 详解

active connections:活跃的连接数量;
server accepts handled requests:总共处理n个连接,成功创建n次握手,共处理n个请求;
reading:读取客户端的连接数;
writing:响应数据到客户端的数量;
waiting:开启keep-alive的情况下,这个值等于active-(reading+writing),意思就是Nginx已经处理完正在等候下一次指令的驻留地址。

五、nginx设置访问用户功能

修改配置文件

[root@localhost ~]# yum -y install httpd-tools  ###安装需要的工具
[root@localhost ~]# htpasswd -c /usr/local/nginx/passwd.db lili ###创建访问用户
New password:
Re-type new password:
Adding password for user lili
[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
  location / {
            root   html;
            index  index.html index.htm;
            allow 192.168.247.160/24; ###允许本机访问
            #deny all; ###拒绝所有,会导致全部无法访问
            auth_basic "secret"; ###验证方式为密码验证
            auth_basic_user_file /usr/local/nginx/passwd.db; ###密码所在文件
        }
[root@localhost ~]# nginx -t ###验证配置文件是否有错
nginx: [warn] low address bits of 192.168.247.160/24 are meaningless in /usr/local/nginx/conf/nginx.conf:48
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost ~]# systemctl restart nginx ###重启服务

验证,访问本地在这里插入图片描述
输入lili和密码
在这里插入图片描述
进入网页
在这里插入图片描述

六、虚拟主机配置

6.1 基于域名

建立网页测试文件

[root@client1 ~]# mkdir -p /var/www/web1/index.html
[root@client1 ~]# mkdir -p /var/www/web2/index.html
[root@client1 ~]# echo "web1" > /var/www/web1/index.html
[root@client1 ~]# echo "web2" > /var/www/web2/index.html

修改配置文件

vi /usr/local/nginx/conf/nginx.conf
 server {
        listen       80;
        server_name  www.aa.com;
        charset utf-8;        
        location / {
            root   /var/www/web1;
            index  index.html index.htm;
            }
        }      
     server {   #添加一个server模块
        listen       80;
        server_name  www.ab.com;
        charset utf-8;
        location / {
            root   /var/www/web2;
            index  index.html index.htm;
            }
        }
[root@localhost ~]# nginx -t ###检查语法
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost ~]# systemctl restart nginx ###重启nginx服务
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
[root@localhost ~]# vi /etc/hosts ###增加映射
192.168.247.160 www.aa.com www.ab.com

验证(可能需要重启主机)
分别访问www.aa.com、www.ab.com

4.2 基于IP

新增一张网卡,添加网卡配置文件
修改配置文件

[root@www ~]# vi /usr/local/nginx/conf/nginx.conf
 server {
        listen       192.168.247.160:80; ###修改IP地址
        server_name  www.aa.com;
        charset utf-8;
        #access_log  logs/aa.access.log  main;
        location / {
            root   /var/www/web1;
            index  index.html index.htm;
            }
        }
     server {
        listen       192.168.247.166:80; ###修改IP地址
        server_name  www.aa.com;
        charset utf-8;
        #access_log  logs/aa.access.log  main;
        location / {
            root   /var/www/web2;
            index  index.html index.htm;
            }
         }
[root@www ~]# nginx -t ###检查语法
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@www ~]# systemctl restart nginx
[root@www ~]# vi /etc/hosts
192.168.73.40 www.aa.com
192.168.73.134 www.aa.com

验证,分别访问192.168.247.160,192.168.247.166

4.3 基于端口

[root@www ~]# vi /usr/local/nginx/conf/nginx.conf
server {
        listen       192.168.247.160:80; ###更改端口号
        server_name  www.aa.com;
        charset utf-8;
        #access_log  logs/aa.access.log  main;
        location / {
            root   /var/www/web1;
            index  index.html index.htm;
            }
        }
     server {
        listen       192.168.247.160:8080; ###更改端口号
        server_name  www.aa.com;
        charset utf-8;
        #access_log  logs/aa.access.log  main;
        location / {
            root   /var/www/web2;
            index  index.html index.htm;
            }
         }
[root@www ~]# systemctl restart nginx

验证,分别访问192.168.247.160、192.168.247.160:8080

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Nginx 是一款轻量级的 Web 服务器软件,它的性能非常出色,可以处理大量的并发请求。以下是 Nginx部署配置步骤: 1. 下载 Nginx:可以到官网下载 Nginx 的源码包,也可以使用包管理器安装。 2. 安装 Nginx:如果使用源码包安装,解压后进入 Nginx 目录,执行以下命令: ``` ./configure make sudo make install ``` 如果使用包管理器安装,可以执行以下命令: ``` sudo apt-get update sudo apt-get install nginx ``` 3. 配置 NginxNginx配置文件位于 /etc/nginx/nginx.conf。可以通过修改配置文件来配置 Nginx。以下是一些常用的配置项: ``` http { server { listen 80; server_name example.com; location / { root /var/www/example.com; index index.html; } } } ``` 上面的配置项表示监听 80 端口,当访问 example.com 时,返回 /var/www/example.com 目录下的 index.html 文件。 4. 启动 Nginx:执行以下命令启动 Nginx: ``` sudo nginx ``` 如果成功启动,可以通过浏览器访问服务器的 IP 地址或域名,验证是否能够正常访问。也可以执行以下命令查看 Nginx 的状态: ``` sudo systemctl status nginx ``` 以上是 Nginx部署配置步骤,具体的配置需要根据实际需求进行调整。 ### 回答2: Nginx是一款轻量级的高性能Web服务器和反向代理服务器,在部署配置过程中,主要需要进行以下步骤: 1. 下载和安装Nginx:访问Nginx官方网站,下载适用于您的操作系统的Nginx安装包,并按照官方文档的指引进行安装。 2. 配置Nginx:默认情况下,Nginx配置文件位于"/etc/nginx/nginx.conf"。可以使用任何文本编辑器打开该文件,根据需求进行配置。 3. 配置虚拟主机Nginx支持虚拟主机配置,可以同时运行多个网站。在配置文件中,可以为每个虚拟主机设置独立的配置,包括域名、端口等信息。 4. 配置反向代理:Nginx的反向代理功能非常强大,可以将客户端的请求转发给后端服务器处理。在配置文件中,需要设置反向代理的目标服务器IP和端口。 5. 配置HTTPS:如果需要使用HTTPS协议进行安全连接,可以在配置文件中添加SSL证书的相关配置,并将HTTP请求转发到HTTPS端口。 6. 优化性能:为了提高Nginx的性能,可以进行一些优化措施,例如调整worker进程数量、设置缓存等。 7. 重载配置:在完成配置文件的修改后,使用命令"nginx -s reload"重载配置文件,以使修改生效。 8. 监控和日志记录:Nginx提供了丰富的监控和日志记录功能,可以通过配置文件设置相关选项,包括访问日志、错误日志等。 总的来说,Nginx部署配置相对简单,但需要根据实际需求进行相应的调整。熟练掌握Nginx配置语法和配置选项,可以帮助提高服务器的性能和安全性。 ### 回答3: Nginx是一个高性能的Web服务器和反向代理服务器,常用于搭建网站和提供静态资源服务。以下是Nginx部署配置过程: 1. 下载和安装Nginx:首先,需要从Nginx官方网站下载适合操作系统的安装包。然后解压安装包,将Nginx文件夹移动到指定位置。 2. 配置NginxNginx配置文件位于安装目录的`/etc/nginx/nginx.conf`。打开配置文件,可以根据需求进行修改。 3. 配置基本设置:在配置文件中,可以设置Nginx监听的端口,默认是80端口。还可以设置后台进程的运行用户和运行模式。 4. 配置虚拟主机:在配置文件中添加虚拟主机配置块,可以实现多域名和多站点的支持。配置块包含域名、根目录和其他需要的参数。 5. 配置反向代理:如果需要使用Nginx作为反向代理服务器,可以在配置文件中添加`proxy_pass`指令,将请求转发到后端服务器上。 6. 配置负载均衡:如果需要实现负载均衡,可以使用`upstream`指令定义后端服务器的IP和端口,并在反向代理配置中使用该`upstream`。 7. 配置缓存和压缩:为了提高性能,可以将静态资源进行缓存和压缩。在配置文件中添加`proxy_cache`和`gzip`指令,启用缓存和压缩功能。 8. 重启Nginx:完成配置后,使用命令行执行`nginx -t`检查配置文件语法是否正确,然后再执行`nginx -s reload`重启Nginx服务。 9. 监控和调试:Nginx提供了丰富的监控指令和日志文件,可以通过查看日志定位问题并进行调试。可以使用命令行执行`tail -f /var/log/nginx/access.log`实时查看访问日志。 以上是Nginx部署配置过程。根据具体需求,可以根据配置文件中的不同参数进行灵活调整。Nginx具有高性能和可靠性,适用于大多数Web应用程序的部署

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值