Nginx配置与虚拟主机

关于Nginx

Nginx是一款高性能、轻量级Web服务软件。具有以下优点:

  • 稳定性高
  • 系统资源消耗低(占有内存少)
  • 对HTTP并发连接的处理能力高(单台物理服务器可支持30000 ~ 50000个并发请求)

编译安装Nginx服务

安装依赖包

yum -y install gcc gcc-c++ pcre-devel zlib-devel make

编译安装Nginx

tar zxvf nginx-1.12.2.tar.gz
cd /opt/nginx-1.12.2/
./configure \
--prefix=/usr/local/nginx \                       #指定安装路径
--user=nginx \                                    #创建一个管理用户
--group=nginx \                                   #管理用户的组
--with-http_stub_status_module                    #开启状态统计模块
make && make install               
useradd -M -s /sbin/nologin nginx                 #创建运行nginx用户、组
ln -s /usr/local/nginx/sbin/* /usr/local/sbin/    #路径优化

停止nginx服务

kill -3  【PID号】:暴力杀死
kill -s QUI’T 【PID号】: 友好杀死
killall -3 nginx
killall -s QUIT nginx

重载

kill -1 【PID号】
kill -s HUP 【PID号】
killall -1 nginx
killall -s HUP 【PID号】

添加Nginx 系统服务(service)

vim /etc/init.d/nginx
#!/bin/bash
# chkconfig: - 99 20             # chkcofig - “-” 表示不启用开机启动管理 (同时 若不加“#”, chkconfig add nginx 会加载不到配置)
# description: Nginx Service Control Script     
COM="/usr/local/nginx/sbin/nginx"	     #命令程序文件位置(nginx)
PID="/usr/local/nginx/logs/nginx.pid"	 #pid文件
case "$1" in   
start)
   $COM
   ;;
stop)
   kill -s QUIT $(cat $PID)
   ;;
restart)
   $0 stop                                        
   $0 start                                      
   ;;
reload)
   kill -s HUP $(cat $PID)
   ;;
*)
       echo "Usage: $0 {start|stop|restart|reload}"
       exit 1
esac
exit 0

chmod +x /etc/init.d/nginx
chkconfig --add nginx   	                      #添加为系统服务
service nginx start		 	                      #开启服务
可在/etc/rc.d/init.d目录下查看到nginx服务

方法二:使用systenctl管理

vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx		                         #描述
After=network.target		                     #描述服务类别
[Service]
Type=forking		                             #后台运行类型
PIDFile =/usr/local/nginx/logs/nginx.pid		 #PID文件位置
ExecStart=/usr/local/nginx/sbin/nginx		     #启动服务
ExecReload=/bin/kill -s HUP $MAINPID		     #根据PID重载配置
ExecStop=/bin/kill -s QUIT $MAINPID		         #根据PID终止进程
PrivateTmp=true						             #开启
[Install]
WantedBy=multi-user.target			             #启动级别

chmod 754 /lib/systemd/system/nginx.service	     #设置754权限是一种安全优化
systemctl start nginx.service
systemctl enable nginx.service

Nginx 配置文件

cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak	
#备份	
cat /usr/local/nginx/conf/nginx.conf                     #查看nginx配置文件
	#user nobody;		                                 #默认运行/管理用户
	worker processes1;		                             #工作进程运行数量,可配置成服务器内核数*2,如果网站访问量不大,一般设为1
	#error_ log  logs/error .log;   		             #错误日志文件路径/级别 	
	#error_ log	logs/error. log	notice;		             
	#error_ log	logs|/error. log	info;
	#pid		logs/ nginx.pid;				         #pid文件位置
events {						                        
	worker connections 1024; 		                     #每个进程最多处理的连接数量

http {												     #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"
	#									' "Shttp_ user_ agent" "$http_ x_ forwarded_ for" ';
	#access_ log	logs/access.log main;		         #访问日志位置
	sendfile	on;							         #支持文件发送(下载)
	#tcp_ nopush	on;						
#此项允许或禁止使用socket的TCP_CORK的选项(发送数据包前先缓存数据),此选项仅在使sendfile的时候使用
	#keepalive_ timeout 0;				                 #连接保持超时时间,单位:秒
	keepalive_timeout 65; 
	#gzip on; 							                 #压缩模块on表示开启
                                                               
   server {											     #web服务相关的一些配置
	listen 		80;								         #默认监听端口
	server name  www.dzw.com;			                 #站点域名;在本地没有dns服务下,可以支持域名访问,改成自己想要的域名
	#charset koi8-r;							         #字符集支持( 修改为中文) UTF-8
	#access_ log logs/host.access.1og main;	             #此web服务的主访问日志只保存htpd服务的访问日志
	location / {									     #“/"根目录配置( 浏览器中,www. baidu. com./
	root		html;								     #网页的目录文件;网站根目录的位置/usr/local/nginx/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   
#以下是支持PHP及跳转的配置
#
#location ~ \.php$ {
# 		proxy_ _pass	http; L /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 conf iguration
#server{					                            #虚拟主机的配置
#	listen	8000;
#	listen	somename: 8080; 
server name	somename	alias 	another .alias;
#	location / {
#	root		html;
#	index 	index.html index. htm;
#
#}
                           
                           nginx加密模块:以注释的方式给出模板
# HTTPS server										     #HTTPS的配置
#
#server {
#	listen				443 ssl;
#	server name			localhost; 
#	ssl certi ficate 			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;
#
}
#}

配置本地映射

vim /etc/hosts
192.168.220.35 www.dzw.com  

访问域名 www.dzw.com

访问状态统计

nginx -V                   #查看已安装的Nginx是否包含HTTP_ STUB STATUS 模块

修改nginx.conf 配置文件,指定访问位置并添加stub_ status 配置

cd /usr/local/nginx/conf/
vim /usr/local/nginx/conf/nginx.conf

在http内的server字段中匹配根目录下添加访问状态统计模块

浏览器访问http://192.168.220.35/status或www.dzw.com/status

  • Active connections :表示当前的活动连接数;
  • service accepts handled requests:表示已经处理的连接信息,三个数字依次表示已处理的连接数、成功的TCP握手次数、已处理的请求数

访问控制

htpawwd : htpasswd是一个用于目录访问权限认证的一个工具。
-c :创建密码文件,如果文件存在,那么内容被清空重写

生成用户密码认证文件:

yum install -y httpd-tools
htpasswd -c /usr/local/nginx/passwd.db wangwu     #passwd.db:表示密码数据文件;用户可以不是系统用户
chown nginx /usr/local/nginx/passwd.db   	      #添加nginx管理、
chmod 400 /usr/local/nginx/passwd.db		      #给与400权限

修改主配置文件相对应目录,添加认证配置项

vim /usr/local/nginx/conf/nginx.conf
 location / {
                auth_basic "secret";		                        #在主页配置项中添加认证
                auth_basic_user_file /usr/local/nginx/passwd.db;    #在主页配置项中添加认证
            root   html;
            index  index.html index.htm;
        }

重启服务,访问测试

测试:浏览器访问http:/ /www.dzw.com,页面提示需要输入账号密码

基于客户端的访问控制

访问控制规则如下:deny IP/IP段:拒绝某个IP或IP段的客户端访问
                                allow IP/IP段: 允许某个IP或IP段的客户端的访问
                                规则从上往下执行,如匹配则停止,不再往下匹配

vim /usr/local /nginx/ conf/ nginx. conf
location / {
root	html ;
index	index.html index .htm; 
deny 192.168.220.5;		                   #添加拒绝访问的客户端的IP
allow all;					               #添加允许其他IP客户端访问

虚拟主机

基于域名的Nginx虚拟主机:

                                   添加域名解析
vim /etc/hosts
192.168.220.35 www.dzw.com www.dzw1.com www.dzw2.com

                                  准备虚拟站点网页文档
mkdir -p /var/www/html/dzw1
mkdir -p /var/www/html/dzw2
echo "<h1> www.dzw1.com </h1>" > /var/www/html/dzw1/index.html
echo "<h1> www.dzw2.com </h1>" > /var/www/html/dzw2/index.html


                        修改配置文件,删除pho跳转部分模板
...
http
...
server {
      	  listen       80;
     	   server_name  www.dzw1.com;
     	   charset utf-8;
      	  access_log  logs/dzw1.access.log;
      	  location / {
          	  root   /var/www/html/dzw1;
           	 index  index.html index.htm;
       	  }
      	  error_page   500 502 503 504  /50x.html;
      	  location = /50x.html {
        	    root   html;
      	  }
}
  	  server {
      	  listen       80;
      	  server_name  www.dzw2.com;
       	 charset utf-8;
       	 access_log  logs/dzw2.access.log;
       	 location / {
         	   root   /var/www/html/dzw2;
          	  index  index.html index.htm;
       	  }
      	 error_page   500 502 503 504  /50x.html;
      	  location = /50x.html {
          	  root   html;
       	 }
}
----wq

systemctl restart nginx                #重启服务

基于端口的虚拟主机:

                                    创建8080端口的网页文件

mkdir -p /var/www/html/ll8080
echo "<h1> www.ll8080.com </h1>" > /var/www/html/ll8080/index.html

                           进入配置文件复制www.dzw1.com的配置修改端口号

   server {							                   #原dzw1配置
        listen       192.168.220.35:80;   		       #指向监听端口
        server_name  www.dzw1.com;
        charset utf-8;
        access_log  logs/dzw1.access.log;
        location / {
            root   /var/www/html/dzw1;
            index  index.html index.htm;
         }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
}

    server {
        listen       192.168.220.35:8080;
        server_name  www.dzw1.com;
        charset utf-8;
        access_log  logs/dzw1.access.log;
        location / {
            root   /var/www/html/dzw1;
            index  index.html index.htm;
         }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
nginx -t
systemctl stop nginx
systemctl start  nginx
netstat -antp | grep nginx

基于不同IP访问:

                             添加192.168.220.100的映射

vim /etc/hosts
192.168.220.100 www.dzw2.com   

           创建网站根目录、创建192.168.220.100的网站首页文件( index. html )

mkdir /var/www/html/dzw2100
echo "<h1> www.dzw2100.com </h1>" /var/www/html/dzw2100/index.html
<h1> www.dzw2100.com </h1> /var/www/html/dzw2100/index.html

                             临时创建虚拟网卡

ifconfig ens33:0 192.168.220.100 netmask 255.255.255.0

                               修改配置文件
 server {
        listen       192.168.220.35:80;
        server_name  www.dzw1.com;
        charset utf-8;
        access_log  logs/dzw1.access.log;
        location / {
            root   /var/www/html/dzw1;
            index  index.html index.htm;
         }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
}

    server {
        listen      192.168.220.100:80;		       #监听ip改为100
        server_name  www.dzw2.com;
        charset utf-8;
        access_log  logs/dzw2.access.log;
        location / {
            root   /var/www/html/dzw2;
            index  index.html index.htm;
         }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值