1,nginx配置讲解

1,Nginx的三大功能:
	1,WWW web服务
	2,负载均衡(反向代理proxy)
	3,web cache (web 缓存)

2,nginx 并发支持的参考值:
	Nginx 并发参考值:1-3w,2W并发,10个进程
	mysql 并发参考值:300-1000
	php   并发参考值:300-1000

3,nginx虚拟主机:
一个server就是一个虚拟主机。
	1,基于域名的虚拟主机。===》应用:外部网站
	2,基于端口的虚拟主机。===》应用:外部网站的后台,公司内部网站
	3,基于IP的虚拟主机。几乎不用。


4,基于域名的虚拟主机
worker_processes 2;
events {
    worker_connections 1024;
}

http {
    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
    sendfile            on;
    keepalive_timeout   65;

    server {
        listen       80;
        server_name  www.etiantian.org;
        root         html/www;
        index index.html index.htm;
    }

    server {
	listen 80;
	server_name  bbs.etiantian.org;
	root html/bbs;
        index index.html index.htm;
     }

    server {
	listen 80;
	server_name  blog.etiantian.org;
	root html/blog;
	index index.html index.htm;
     }
}
验证得到:
[root@centos7 nginx]# curl 127.0.0.1  #直接IP访问,默认访问的是配置文件的第一个虚拟主机
www.etiantian.org
[root@centos7 nginx]#


[root@centos7 html]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.0.11 blog.etiantian.org www.status.org bbs.etiantian.org www.etiantian.org
[root@centos7 html]# 
[root@centos7 ~]# curl blog.etiantian.org  #通过域名和hosts 绑定的,会根据访问的域名去查找域名对应的虚拟主机
blog.etiantian.org
[root@centos7 ~]# curl www.etiantian.org
www.etiantian.org
[root@centos7 ~]# curl bbs.etiantian.org
bbs.etiantian.org
[root@centos7 ~]# 
[root@centos7 ~]# curl www.status.org
Active connections: 1 
server accepts handled requests
 18 18 18 
Reading: 0 Writing: 1 Waiting: 0 
[root@centos7 ~]# 


5,别名:
空格的形式实现别名:
    server {
        listen       80;
        server_name  www.etiantian.org  etiantian.org;  #别名访问浏览器访问的地址不会改变
        root         html/www;
        index index.html index.htm;
    }

[root@centos7 nginx]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.0.11 etiantian.org blog.etiantian.org www.status.org bbs.etiantian.org www.etiantian.org
[root@centos7 nginx]# curl www.etiantian.org
www.etiantian.org
[root@centos7 nginx]# curl etiantian.org
www.etiantian.org
[root@centos7 nginx]# 

6,301重定向:
rewrite访问浏览器地址会改变:
server {
        listen       80;
        server_name  etiantian.org;
	rewrite ^/(.*) http://bbs.etiantian.org/$1 permanent;
    }


7,基于端口的虚拟主机:
[root@centos7 nginx]# cat nginx.conf

worker_processes 2;
events {
    worker_connections 1024;
}

http {
    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
    sendfile            on;
    keepalive_timeout   65;

    server {
        listen       8000;
        server_name  etiantian.org;
	rewrite ^/(.*) http://bbs.etiantian.org/$1 permanent;
    }
    server {
        listen       80;
        server_name  www.etiantian.org;
        root         html/www;
        index index.html index.htm;
    }

     server {
	listen 8001;
	server_name  bbs.etiantian.org;
	root html/bbs;
        index index.html index.htm;
     }

     server {
	listen 8002;
	server_name  blog.etiantian.org;
	root html/blog;
	index index.html index.htm;
     }
    
    server {
        listen 8003;
        server_name  www.status.org;
        stub_status on;
     }

}

[root@centos7 nginx]# 

8,基于IP的:
[root@centos7 nginx]# cat nginx.conf

worker_processes 2;
events {
    worker_connections 1024;
}

http {
    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
    sendfile            on;
    keepalive_timeout   65;

    server {
        listen       192.168.0.11:8000;
        server_name  _;
        root         html/www;
        index index.html index.htm;
    }
}
[root@centos7 nginx]# 

9,nginx 负载均衡
[root@centos7 nginx]# cat nginx.conf

worker_processes 2;
events {
    worker_connections 1024;
}

http {
    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
    sendfile            on;
    keepalive_timeout   65;

    upstream backend {
	server 192.168.0.13:80 max_fails=3 fail_timeout=30s;
	server 192.168.0.14:80 max_fails=3 fail_timeout=30s; 
    }
    server {
        listen 80;
        server_name  www.etiantian.com;
        index index.html index.htm;
	location / {
		proxy_pass http://backend;
	}
     }

}
[root@centos7 nginx]# 

10,
在后端主机13执行:
[root@centos7 nginx]# 
[root@centos7 nginx]# curl bbs.etiantian.org
bbs.etiantian.org
[root@centos7 nginx]# curl blog.etiantian.org
blog.etiantian.org
[root@centos7 nginx]# hostname -i
192.168.0.13
[root@centos7 nginx]#

在后端主机14执行:
[root@centos7 nginx]# 
[root@centos7 nginx]# curl bbs.etiantian.org
bbs.etiantian.org
[root@centos7 nginx]# curl blog.etiantian.org
blog.etiantian.org
[root@centos7 nginx]# hostname -i
192.168.0.14
[root@centos7 nginx]#

在负载均衡器执行:(为什么总是代理到后端主机的第一个虚拟主机呢?)
[root@centos7 nginx]# cat nginx.conf

worker_processes 2;
events {
    worker_connections 1024;
}

http {
    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
    sendfile            on;
    keepalive_timeout   65;

    upstream backend {
	server 192.168.0.13:80 max_fails=3 fail_timeout=30s;
	server 192.168.0.14:80 max_fails=3 fail_timeout=30s; 
    }
    server {
        listen 80;
        server_name  www.etiantian.org;
        index index.html index.htm;
	location / {
		proxy_pass http://backend;
	}
     }
    
    server {
        listen 80;
        server_name  bbs.etiantian.org;
        index index.html index.htm;
        location / {
                proxy_pass http://backend;
        }
     }


}

[root@centos7 nginx]# 
[root@centos7 nginx]# 
[root@centos7 nginx]# curl www.etiantian.org
bbs.etiantian.org
[root@centos7 nginx]# curl blog.etiantian.org
bbs.etiantian.org
[root@centos7 nginx]# 

解决办法:
添加:
proxy_set_header Host $host;

    server {
        listen 80;
        server_name  bbs.etiantian.org;
        index index.html index.htm;
        location / {
                proxy_pass http://backend;
		proxy_set_header Host $host;
        }
     }
[root@centos7 nginx]# curl bbs.etiantian.org
bbs.etiantian.org
[root@centos7 nginx]# 
访问正常。

  

posted @ 2020-07-21 23:46  pwcc  阅读(237)  评论(0)    收藏  举报