nginx 端口域名IP+其余配置
虚拟主机
Nginx配置虚拟主机有如下三种方式:
方式一、基于主机多IP方式
方式二、基于端口的配置方式
方式三、基于多个hosts名称方式(多域名方式)
多IP虚拟主机

ip addr add 10.0.0.11/24 dev eth0
ip addr add 10.0.0.12/24 dev eth0
[root@web01 code]
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP
group default qlen 1000
link/ether 00:0c:29:1d:4d:9a brd ff:ff:ff:ff:ff:ff
inet 10.0.0.7/24 brd 10.0.0.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
inet 10.0.0.11/24 scope global secondary eth0
valid_lft forever preferred_lft forever
inet 10.0.0.12/24 scope global secondary eth0
valid_lft forever preferred_lft forever
vim /etc/nginx/conf.d/7.conf
server{
listen 80;
server_name 10.0.0.7;
root /7;
index index.html;
}
vim /etc/nginx/conf.d/11.conf
server{
listen 80;
server_name 10.0.0.11;
root /11;
index index.html;
}
vim /etc/nginx/conf.d/12.conf
server{
listen 80;
server_name 10.0.0.12;
root /12;
index index.html;
}
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 conf.d]
[root@web01 conf.d]
[root@web01 conf.d]
[root@web01 conf.d]
[root@web01 conf.d]
[root@web01 conf.d]
systemctl restart nginx
systemctl reload nginx
10.0.0.7
10.0.0.11
10.0.0.12
多端口的配置方式
vim /etc/nginx/conf.d/1.conf
server{
listen 80;
server_name 10.0.0.7;
root /7;
index index.html;
}
vim /etc/nginx/conf.d/2.conf
server{
listen 90;
server_name 10.0.0.7;
root /11;
index index.html;
}
vim /etc/nginx/conf.d/3.conf
server{
listen 8080;
server_name 10.0.0.7;
root /12;
index index.html;
}
nginx -t
systemctl restart nginx
[root@web01 conf.d]
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 20935/nginx: master
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 20935/nginx: master
[root@web01 conf.d]
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 9028/php-fpm: maste
tcp 0 0 0.0.0.0:90 0.0.0.0:* LISTEN 20935/nginx: master
[root@web01 conf.d]
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 20935/nginx: master
[root@web01 conf.d]
[root@web01 conf.d]
10.0.0.7
10.0.0.7:90
10.0.0.7:8080
比如 3306 9000 22
多域名模式
vim /etc/nginx/conf.d/1.conf
server{
listen 80;
server_name 1.hh.com;
root /7;
index index.html;
}
vim /etc/nginx/conf.d/2.conf
server{
listen 80;
server_name 2.hh.com;
root /11;
index index.html;
}
vim /etc/nginx/conf.d/3.conf
server{
listen 80;
server_name 3.hh.com;
root /12;
index index.html;
}
nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
systemctl restart nginx
10.0.0.7 1.hh.com 2.hh.com 3.hh.com
(C:\Windows\System32\drivers\etc\hosts
http://1.hh.com/
http://2.hh.com/
http://3.hh.com/
nginx模块(常用)
在官网(nginx.org)的 右侧的documentation(评注)下

(index.html)目录索引模块
vim /etc/nginx/conf.d/test.conf
server{
listen 80;
server_name 10.0.0.7;
root /test;
index index.html;
}
mkdir /test
[root@web01 conf.d]
[root@web01 conf.d]
test
nginx -t
systemctl restart nginx
10.0.0.7

(autoindex)配置目录索引模块
当 ngx_http_index_module 模块找不到索引文件时,通常会将请求传递给
ngx_http_autoindex_module 模块。
Syntax: autoindex on | off;
Default:
autoindex off;
Context: http, server, location
off 显示文件的大概大小
on 显示文件的具体大小
server{
listen 80;
server_name 10.0.0.7;
location / {
root /test;
index index.html;
autoindex on;
}
}
nginx -t
systemctl restart nginx
mv /test/index.html /opt
10.0.0.7

Syntax: autoindex_format html | xml | json | jsonp;
Default:
autoindex_format html;
Context: http, server, location
server{
listen 80;
server_name 10.0.0.7;
location / {
root /test;
index index.html;
autoindex on;
autoindex_exact_size on;
autoindex_format xml;
}
}
nginx -t
systemctl restart nginx
10.0.0.7

(autoindex_localtime)时间显示模式
Syntax: autoindex_localtime on | off;
Default:
autoindex_localtime off;
Context: http, server, location
on 现实的是找点目录下文件的属性时间
off 显示的是格林威治时间
server{
listen 80;
server_name 10.0.0.7;
location / {
root /test;
index index.html;
autoindex on;
autoindex_exact_size on;
autoindex_format html;
autoindex_localtime on;
}
}
nginx -t
systemctl restart nginx
10.0.0.7

(stub_status)nginx状态模块
server{
listen 80;
server_name 10.0.0.7;
location / {
root /test;
index index.html;
autoindex on;
autoindex_exact_size on;
autoindex_format html;
autoindex_localtime on;
}
location = /aaa {
stub_status;
}
}
nginx -t
systemctl restart nginx
10.0.0.7/aaa
Active connections: 2
accepts
handled
requests
4 4 51
Reading: 0
Writing: 1
Waiting: 1
一次tcp请求里面 可以发起多次http的请求
keepalive_timeout 0; 关闭了长连接
keepalive_timeout 65; 65s内网页没有收到请求书就断开tcp连接

(auth_basic)用户的访问控制
location / {
auth_basic "closed site";
auth_basic_user_file conf/htpasswd;
}
Syntax: auth_basic string | off;
Default:
auth_basic off;
Context: http, server, location, limit_except
yum install -y httpd
mkdir /etc/nginx/auth
htpasswd -b -c /etc/nginx/auth/xxx.auth xxx 123
-b
-c
[root@web01 auth]
xxx:$apr1$LYCDPF82$dFTfxcThQDnQficauLgZ9.
server{
listen 80;
server_name 10.0.0.7;
root /test;
index index.html;
location = /aaa {
stub_status;
auth_basic "input your username & password";
auth_basic_user_file /etc/nginx/auth/xxx.auth;
}
}
nginx -t
systemctl restart nginx
10.0.0.7/aaa
(syntax)用户访问限制
Syntax: allow address | CIDR | unix: | all;
Default: —
Context: http, server, location, limit_except
Syntax: deny address | CIDR | unix: | all;
Default: —
Context: http, server, location, limit_except
10.0.0.0/24 255.255.255.0
10.0.0.0/16 255.255.0.0
server{
listen 80;
server_name _;
root /test;
index index.html;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
allow 10.0.0.9;
deny all;
}