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 段:
1 http { 2 include mime.types; 3 default_type application/octet-stream; 4 5 #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' 6 # '$status $body_bytes_sent "$http_referer" ' 7 # '"$http_user_agent" "$http_x_forwarded_for"'; 8 9 #access_log logs/access.log main; 10 11 sendfile on; 12 #tcp_nopush on; 13 14 #keepalive_timeout 0; 15 keepalive_timeout 65; 16 17 #gzip on; 18 19 server { 20 listen 80; 21 server_name localhost; 22 23 #charset koi8-r; 24 25 #access_log logs/host.access.log main; 26 27 location / { 28 root html; 29 index index.html index.htm; 30 } 31 32 #error_page 404 /404.html; 33 34 # redirect server error pages to the static page /50x.html 35 # 36 error_page 500 502 503 504 /50x.html; 37 location = /50x.html { 38 root html; 39 } 40 41 # proxy the PHP scripts to Apache listening on 127.0.0.1:80 42 # 43 #location ~ .php$ { 44 # proxy_pass http://127.0.0.1; 45 #} 46 47 # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 48 # 49 #location ~ .php$ { 50 # root html; 51 # fastcgi_pass 127.0.0.1:9000; 52 # fastcgi_index index.php; 53 # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; 54 # include fastcgi_params; 55 #} 56 57 # deny access to .htaccess files, if Apache's document root 58 # concurs with nginx's one 59 # 60 #location ~ /.ht { 61 # deny all; 62 #} 63 } 64 65 66 # another virtual host using mix of IP-, name-, and port-based configuration 67 # 68 #server { 69 # listen 8000; 70 # listen somename:8080; 71 # server_name somename alias another.alias; 72 73 # location / { 74 # root html; 75 # index index.html index.htm; 76 # } 77 #} 78 79 80 # HTTPS server 81 # 82 #server { 83 # listen 443 ssl; 84 # server_name localhost; 85 86 # ssl_certificate cert.pem; 87 # ssl_certificate_key cert.key; 88 89 # ssl_session_cache shared:SSL:1m; 90 # ssl_session_timeout 5m; 91 92 # ssl_ciphers HIGH:!aNULL:!MD5; 93 # ssl_prefer_server_ciphers on; 94 95 # location / { 96 # root html; 97 # index index.html index.htm; 98 # } 99 #} 100 101 }
配置 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 80; 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;