cat /etc/nginx/nginx.confworker_processes 1;#定义有多少个工作的子进程,可自行修改,太大无益,因为要争夺CPU,一般设置为核心总数(lscpu中CPU(S)可看)events { worker_connections 1024; #一个worker允许同时最大产生多少个链接}http { #Web功能的标签 include conf/mime.types; #设定mime类型,类型由conf/mime.types决定 default_type application/octet-stream; sendfile on; #sendfile指令指定 nginx 是否调用sendfile 函数(zero copy 方式)来输出文件,对于普通应用,必须设 为on。如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络IO处理速度,降低系统uptime。 keepalive_timeout 65; #http长连接的断开时间,(长连接:一次建立TCP链接多次请求)从最后一次请求开始计算时间 server { #虚拟主机的标签 listen 80; #侦听的端口 server_name localhost; #服务器的名称,别人可以通过这个来访问你的网站 location / { root html; #网站的根目录,如果是绝对路径,就是对于nginx服务器的家目录来说的 index index.html index.htm; #网站默认的查找首页,从左向右依次查找 } error_page 500 502 503 504 /50x.html; #如果碰到上述状态码,则转交给后面的50x.html页面 location = /50x.html { root html; } }}
IP address-based virtual host
在http标签里面添加上一对server标签 server { listen 80; server_name 10.0.0.7; access_log logs/ip_access.log main; location / { root html/server_ip; index index.html } }保存退出,检查语法[[email protected]_7 nginx]# nginx -c /usr/local/nginx/conf/nginx.conf -t 2018/08/24 11:15:36 [info] 9888#0: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok2018/08/24 11:15:36 [info] 9888#0: the configuration file /usr/local/nginx/conf/nginx.conf was tested successfully平滑重启(重读配置文件)[[email protected]_7 nginx]# kill -HUP `cat /usr/local/nginx/logs/nginx.pid`创建虚拟主机的网站根目录,刚才在配置文件里面定义的[[email protected]_7 nginx]# mkdir /usr/local/nginx/html/server_ip进入到这个里面编辑网站的主页[[email protected]_7 nginx]# cat /usr/local/nginx/html/server_ip/index.html
Port-based virtual host (this method can be used in the production environment to open the site to a separate background, increase security) Add a pair of server tag server {Listen 8888 to the HTTP tag; Server_namelocalhost; Access_log Logs/8888_port_access.log Main; Location/{root html/port; Index index.html}} save exit, check syntax [[email protected]_7 nginx]# nginx-c/usr/local/nginx/conf/nginx.conf-t 201 8/08/24 11:15:36 [INFO] 9888#0:the configuration file/usr/local/nginx/conf/nginx.conf syntax is ok2018/08/24 11:15:36 [i NFO] 9888#0:the configuration file/usr/local/nginx/conf/nginx.conf was tested successfully smooth restart (reread config file) [[email Protected]_7 nginx]# kill-hup ' cat/usr/local/nginx/logs/nginx.pid ' creates the Web site root of the virtual host, just defined in the configuration file [[email protected ]_7 nginx]# Mkdir/usr/local/nginx/html/port into the home page of the edit website [[email protected]_7 nginx]# cat/usr/local/nginx/ Html/port/index.html
Domain-based virtual host Add a pair of server tag server {Listen 80 to the HTTP tag; server_name www.sentinel.com; Access_log Logs/sentinel_access.log Main; Location/{root Html/sentinel; Index index.html}} save exit, check syntax [[email protected]_7 nginx]# nginx-c/usr/local/nginx/conf/nginx.conf-t 201 8/08/24 11:15:36 [INFO] 9888#0:the configuration file/usr/local/nginx/conf/nginx.conf syntax is ok2018/08/24 11:15:36 [i NFO] 9888#0:the configuration file/usr/local/nginx/conf/nginx.conf was tested successfully smooth restart (reread config file) [[email Protected]_7 nginx]# kill-hup ' cat/usr/local/nginx/logs/nginx.pid ' creates the Web site root of the virtual host, just defined in the configuration file [[email protected ]_7 nginx]# Mkdir/usr/local/nginx/html/sentinel into the home page of the edit website [[email protected]_7 nginx]# Cat/usr/local/nginx /html/sentinel/index.html
Nginx.conf and Nginx virtual hosts