Nginx 重启的另外一种方式,相当于 kill -HUP `cat /usr/local/nginx/logs/nginx.pid`:
/usr/local/nginx/sbin/nginx -s reload
停止 Nginx 的另外一种方式:
/usr/local/nginx/sbin/nginx -s stop
重读日志文件的另一种方式,相当于 kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`:
/usr/local/nginx/sbin/nginx -s reopen
测试配置文件是否正确:
/usr/local/nginx/sbin/nginx -t
如果配置正确,则会提示 successful:
虚拟主机的管理
vim /usr/local/nginx/conf/nginx.conf
全局区:
worker_processes 1 表示有 1 个工作的子进程,可以进行修改,但是过大没有意义,因为需要占用更多的 CPU 资源,一般设置为 CPU 数 * 核数
events 区:
一般配置 Nginx 进程与连接的特性。worker_connections 1024 表示 1 个子进程(worker)最多允许 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" '
# '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on;
#tcp_nopush on; #keepalive_timeout ;
keepalive_timeout ; #gzip on; server {
listen ;
server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / {
root html;
index index.html index.htm;
} #error_page /.html; # redirect server error pages to the static page /50x.html
#
error_page /50x.html;
location = /50x.html {
root html;
} # proxy the PHP scripts to Apache listening on 127.0.0.1:
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:;
# 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 configuration
#
#server {
# listen ;
# listen somename:;
# server_name somename alias another.alias; # location / {
# root html;
# index index.html index.htm;
# }
#} # HTTPS server
#
#server {
# listen ssl;
# server_name localhost; # ssl_certificate 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;
# }
#} }
配置 http 服务器的主要的段。
http 段中的 server 段:
server {
listen 80;
server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / {
root 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
#
#location ~ \.php$ {
# proxy_pass http://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;
#}
}
server 就是虚拟主机。
【例1】基于域名的虚拟主机
添加 server 段:
server{
listen 80;
server_name dee.com;
location /{
root dee.com;
index index.html;
}
}
保存退出;
location 可以使用绝对路径,也可以使用相对路径,相对路径是相对于 nginx 的根目录:/usr/local/nginx
在 /usr/local/nginx 目录下创建目录 dee.com,在其下创建 index.html:
[root@localhost ~]# cd /usr/local/nginx/
[root@localhost nginx]# mkdir dee.com
[root@localhost nginx]# vim dee.com/index.html
index.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>dee.com</title>
<body>
Welcome to dee.com
</body>
</head>
</html>
重新读取配置文件:
/usr/local/nginx/sbin/nginx -s reload
配置 hosts:
192.168.254.100 dee.com
访问 dee.com:
【例2】基于 ip 的虚拟主机
编辑配置文件:
vim /usr/local/nginx/conf/nginx.conf
添加 server 段:
server{
listen ;
server_name 192.168.254.100;
location /{
root ip;
index index.html;
}
}
在 /usr/local/nginx 目录下新建 ip 目录:
[root@localhost nginx]# mkdir ip
[root@localhost nginx]# vim ip/index.html
index.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>dee.com</title>
<body>
ip test
</body>
</head>
</html>
访问 http://192.168.254.100/
【例3】基于端口的虚拟主机
编辑配置文件(vim 查看行号 :set nu):
vim /usr/local/nginx/conf/nginx.conf
添加 server 段:
server{
listen 2022;
server_name dee.com;
location /{
root /var/www;
index index.html;
}
}
这时配置 location 使用绝对路径;
在 /var 下新建 www 目录,在其下新建 index.html 文件:
[root@localhost nginx]# mkdir /var/www
[root@localhost nginx]# vim /var/www/index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>dee.com</title>
<body>
Welcome to dee.com's admin panel
</body>
</head>
</html>
重新读取配置文件:
/usr/local/nginx/sbin/nginx -s reload
访问 http://dee.com:2022/
或者访问 http://192.168.254.100:2022/
附:如果需要启用目录浏览,只需要在 Server 段中加入 autoindex on;