基于端口的虚拟主机
[root@web01 /etc/nginx]# cat nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
server {
listen 81;
server_name www.oldboy.com;
location / {
root /usr/share/nginx/html/www;
index index.html index.htm;
}
}
server {
listen 82;
server_name blog.oldboy.com;
location / {
root /usr/share/nginx/html/blog;
index index.html index.htm;
}
}
}
[root@web01 /etc/nginx]# nginx -s reload
[root@web01 /etc/nginx]# ss -lntup |grep nginx
tcp LISTEN 0 128 *:81 : users:(("nginx",pid=9691,fd=10),("nginx",pid=9564,fd=10))
tcp LISTEN 0 128 *:82 : users:(("nginx",pid=9691,fd=11),("nginx",pid=9564,fd=11))
[root@web01 /etc/nginx]# curl http://10.0.0.7
curl: (7) Failed connect to 10.0.0.7:80; Connection refused
[root@web01 /etc/nginx]# curl http://10.0.0.7:81
www.oldboy.com
[root@web01 /etc/nginx]# curl http://10.0.0.7:82
blog.oldboy.com
ip addr add 10.0.0.11/24 dev eth0
基于ip的虚拟主机
[root@web01 /etc/nginx]# cat nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
server {
listen 10.0.0.7:80;
server_name www.oldboy.com;
location / {
root /usr/share/nginx/html/www;
index index.html index.htm;
}
}
server {
listen 10.0.0.9:80;
server_name blog.oldboy.com;
location / {
root /usr/share/nginx/html/blog;
index index.html index.htm;
}
}
}
[root@web01 /etc/nginx]# systemctl restart nginx
[root@web01 /etc/nginx]#
[root@web01 /etc/nginx]#
[root@web01 /etc/nginx]# ss -lntup |grep nginx
tcp LISTEN 0 128 10.0.0.9:80 : users:(("nginx",pid=9785,fd=7),("nginx",pid=9784,fd=7))
tcp LISTEN 0 128 10.0.0.7:80 : users:(("nginx",pid=9785,fd=6),("nginx",pid=9784,fd=6))
[root@web01 /etc/nginx]# curl 10.0.0.7
www.oldboy.com
[root@web01 /etc/nginx]# curl 10.0.0.9
blog.oldboy.com
nginx处理用户请求过程
http://nginx.org/en/docs/http/request_processing.html
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 /var/log/nginx/access.log main;
nginx内置变量 :
'remote_user 远程用户(空)
[request" 请求报文的起始行 status 状态码
http_referer" 记录着用户从哪里跳转过来的
'"http_x_forwarded_for"'; 负载均衡: web服务器用来记录用户真实ip地址
access_log /var/log/nginx/access_www-gzip.log main gzip buffer=16k flush=5s ;
zcat zless zmore zgrep zegrep
[root@web01 /etc/nginx]# cat nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/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"';
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
server {
listen 80;
server_name www.oldboy.com;
access_log /var/log/nginx/access_www.log main ;
location / {
root /usr/share/nginx/html/www;
index index.html index.htm;
}
}
server {
listen 80;
server_name blog.oldboy.com;
access_log /var/log/nginx/access_blog.log main;
location / {
root /usr/share/nginx/html/blog;
index index.html index.htm;
}
}
}
[root@web01 /etc/nginx]# ll /etc/nginx/conf.d/
total 12
-rw-r--r-- 1 root root 211 Jun 5 11:54 01-www.conf
-rw-r--r-- 1 root root 217 Jun 5 11:54 02-blog.conf
-rw-r--r-- 1 root root 488 Apr 23 22:34 default.conf.gz
[root@web01 /etc/nginx]# cat nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/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"';
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
[root@web01 /etc/nginx]#
[root@web01 /etc/nginx]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 /etc/nginx]# systemctl reload nginx
[root@web01 /etc/nginx]# curl www.oldboy.com
www
[root@web01 /etc/nginx]# curl blog.oldboy.com
blog.oldboy.com
192.168.22.43:9000 ---->10.0.0.7:80
nginx状态模块: --with-http_stub_status_module
[root@web01 /etc/nginx/conf.d]# cat status.conf
server {
listen 80;
server_name status.oldboy.com;
stub_status on;
access_log off;
}
[root@web01 /etc/nginx/conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 /etc/nginx/conf.d]# systemctl reload nginx
[root@web01 /etc/nginx/conf.d]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
172.16.1.5 lb01
172.16.1.6 lb02
172.16.1.7 web01 www.oldboy.com blog.oldboy.com bbs.oldboy.com status.oldboy.com
172.16.1.8 web02
172.16.1.31 nfs01
172.16.1.41 backup
172.16.1.51 db01 db01.etiantian.org
172.16.1.61 m01
[root@web01 /etc/nginx/conf.d]# curl status.oldboy.com
Active connections: 1
server accepts handled requests
566 566 1194
Reading: 0 Writing: 1 Waiting: 0
http://192.168.22.43:9000/
错误提示:
Connection refused
连接拒绝
root@web01 /etc/nginx]# curl www.oldboy.com
curl: (7) Failed connect to www.oldboy.com:80; Connection refused
服务 是否运行
Address already in use
nginx正在运行中
[root@web01 ~]# nginx
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()
nginx 可以运行nginx服务
启动 或重启nginx 的报错
查看详细nginx错误提示 检查语法 nginx -t
systemctl
[root@web-204 html]# systemctl start nginx
Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.
[root@web-204 html]# nginx -t
nginx: [emerg] unexpected "}" in /etc/nginx/nginx.conf:49
nginx: configuration file /etc/nginx/nginx.conf test failed
}不成对
Linux或windows 使用域名
hosts(linux或windows) 没有解析
[root@web01 /etc/nginx]# curl blog.oldboy.com
<a href="https://www.afternic.com/forsale/blog.oldboy.com?utm_source=TDFS_DASLNC&utm_medium=DASLNC&utm_campaign=TDFS_DASLNC&traffic_type=TDFS_DASLNC&traffic_id=daslnc&">Found</a>.
server_name 这一行 没有以 ";" 结尾
terminated 结束
[root@wed01 ~]# nginx -t
nginx: [emerg] directive "server_name" is not terminated by ";" in /etc/nginx/nginx.conf:43
nginx: configuration file /etc/nginx/nginx.conf test failed
31 # include /etc/nginx/conf.d/*.conf;
32 server {
33 listen 80;
34 server_name www.oldboy.com;
35 location / {
36 root /usr/share/nginx/html/www;
37 index index.html index.htm;
38 }
39 }
40 server {
41 listen 80;
42 server_name blog.oldboy.com
43 location / {
44 root /usr/share/nginx/html/blog;
45 index index.html index.htm;
46 }
47 }
48
49 }
[root@web01 ~]# cat /user/share/nginx/html/{www,blog}/index.html
www oldboy.com
blog oldboy.com
[root@web01 ~]# vim /etc/hosts
[root@web01 ~]# curl blog.oldboy.com
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<center>nginx/1.16.0</center>
</body>
</html>
[root@web01 ~]# curl www.oldboy.com
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<center>nginx/1.16.0</center>
</body>
</html>
403 Forbidden
没有权限
[root@web01 ~]# cd /usr/share/nginx/html/www/
[root@web01 /usr/share/nginx/html/www]# ll
total 4
-rw-r--r-- 1 root root 15 Jun 5 08:39 index.html
[root@web01 /usr/share/nginx/html/www]# chmod 000 index.html
[root@web01 /usr/share/nginx/html/www]# ll
total 4
---------- 1 root root 15 Jun 5 08:39 index.html
[root@web01 /usr/share/nginx/html/www]# curl www.oldboy.com
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<center>nginx/1.16.0</center>
</body>
</html>
[root@web01 /usr/share/nginx/html/www]# chmod 644 index.html
[root@web01 /usr/share/nginx/html/www]# ll
total 4
-rw-r--r-- 1 root root 15 Jun 5 08:39 index.html
首页文件不存在 默认找首页文件 403
[root@web01 /usr/share/nginx/html/www]# ll
total 0
[root@web01 /usr/share/nginx/html/www]# curl www.oldboy.com
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<center>nginx/1.16.0</center>
</body>
</html>
Cannot assign requested address
无法分配指定的ip地址
本地没有10.0.0.9ip
[root@web01 /etc/nginx]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: [emerg] bind() to 10.0.0.9:80 failed (99: Cannot assign requested address)
nginx: configuration file /etc/nginx/nginx.conf test failed
ip addr add 10.0.0.9/24 dev eth0 label eth0:1
304 Not Modified 用户读取浏览器缓存
conflicting server name
域名冲突: 有两个几个 虚拟主机的域名相同了
[root 12:27:17 @web01 conf.d]# nginx -t
nginx: [warn] conflicting server name "www.oldboy.com" on 0.0.0.0:80, ignored
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root 12:16:28 @web01 ~]# cat /etc/nginx/nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/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"';
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
[root 12:27:22 @web01 conf.d]# vim 01-www.conf
server {
listen 80;
server_name www.oldboy.com;
#charset koi8-r;
access_log /var/log/nginx/access_www.log main;
location / {
root /usr/share/nginx/html/www;
index index.html index.htm;
}
}
[root 12:29:17 @web01 conf.d]# vim 02-blog.conf
server {
listen 80;
server_name blog.oldboy.com;
#charset koi8-r;
access_log /var/log/nginx/access_blog.log main;
location / {
root /usr/share/nginx/html/blog;
index index.html index.htm;
}
}
[root 12:30:38 @web01 conf.d]# cat 03-status.conf
server {
listen 80;
server_name status.oldboy.com;
stub_status on;
access_log off;
}
[root 12:35:02 @web01 conf.d]# zcat default.conf.gz
server {
listen 80;
server_name www.oldboy.com;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
curl -v -H Host:status.oldboy.com 172.16.1.7
状态模块
location规则
lnmp
网友评论