一:使用Nginx搭建虚拟主机服务器时,每个虚拟WEB站点拥有独立的"server {}"配置段,各自监听的IP地址、端口号可以单独指定,当然网站名称也是不同的。
二:虚拟机的分类:
1:基于域名虚拟主机(一个ip地址对应多个域名,不同域名就是不同的站点,其内容也不一样)
2:基于端口虚拟主机(服务器只有一个ip地址,不同端口就是不同的站点,其内容也不一样)
3:基于ip虚拟主机(服务器有多个ip地址,不同ip就是不同的站点,其内容也不一样)
=======================================================================
[[email protected] conf]# vim nginx.conf
user nginx nginx; //nginx的程序账户及程序组 worker_processes 2; //指定进程数一般与cpu数量一致 worker_cpu_affinity 00000001 00000010; //为每个进程分配核心数 error_log logs/error.log info; //全局错误日志文件位置 pid logs/nginx.pid; //PID文件的位置 events { use epoll; //使用epoll模型 worker_connections 10240; //每个进程允许的最多的连接数默认为1024一般10000以下 } 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; //支持文件发送超时 keepalive_timeout 65; server { //web服务的监听配置 listen 80; //监听地址及端口(IP:PORT) server_name www.crushlinux.com; //网站名称(FQDN) charset utf-8; //网页的默认字符集 access_log logs/www.crushlinux.com.access.log main; location / { //根目录配置 root html; //网站根目录的位置安装位置的html中 index index.html index.htm; //默认首页 } location /status { stub_status on; //打开状态统计状态 access_log off; //关闭此位置的日志记录 } error_page 500 502 503 504 /50x.html; //内部错误的反馈页面 location =/50x.html { //错误页面配置 root html; } } }
[[email protected] conf]# pwd
/usr/local/nginx/conf
[[email protected] conf]# mkdir ../html/mailcom
[[email protected] conf]# echo "<h1>www.crushlinux.com</h1>" > ../html/index.html
[[email protected] conf]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: [warn] 10240 worker_connections exceed open file resource limit: 1024
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[[email protected] conf]# ulimit -n 65536
[[email protected] conf]# 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
[[email protected] conf]# systemctl stop firewalld
[[email protected] conf]# iptables -F
[[email protected] conf]# setenforce 0
[[email protected] conf]# systemctl restart nginx
Warning: nginx.service changed on disk. Run ‘systemctl daemon-reload‘ to reload units.
[[email protected] conf]# /etc/init.d/nginx reload
Active connections 表示当前活跃的连接数,
第三行的三个数字表示Nginx当前总共处理了2个连接,成功创建3次握手,总共处理了2个请求。
=======================================================================
要创建两个站点www.crushlinux.com和www.cloud.com
为两个虚拟WEB主机分别建立根目录,并准备测试首页
[[email protected]~]#mkdir /usr/local/nginx/html/crushlinux
[[email protected]~]#mkdir /usr/local/nginx/html/cloud
[[email protected]~]# echo "<h1>www.crushlinux.com</h1>" >/usr/local/nginx/html/crushlinux/index.html
[[email protected]~]# echo "<h1>www.cloud.com</h1>" > /usr/local/nginx/html/cloud/index.html
[[email protected]~]# vim /usr/local/nginx/conf/nginx.conf
user nginx nginx; worker_processes 2; worker_cpu_affinity 00000001 00000010; error_log logs/error.log info; pid logs/nginx.pid; events { use epoll; worker_connections 10240; } 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; keepalive_timeout 65; server { listen 80; server_name www.crushlinux.com; charset utf-8; access_log logs/www.crushlinux.com.access.log main; location / { root html/crushlinux; index index.html index.htm; }
location ~ /status {
stub_status on;
access_log off;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name www.cloud.com;
charset utf-8;
access_log logs/cloud.access.log main;
location / {
root html/cloud;
index index.html index.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
[[email protected] ~]# killall -3 nginx //正常停止
[[email protected] ~]# nginx //正常启动
[[email protected] ~]# yum -y install elinks
[[email protected] ~]# elinks --dump http://www.crushlinux.com
www.crushlinux.com
[[email protected] ~]# elinks --dump http://www.cloud.com
www.cloud.com
原文地址:https://www.cnblogs.com/cxm123123form/p/11601827.html