Nginx配置文件编写实例

编写nginx服务配置文件

nginx配置文件三个语法格式说明:
1. 大括号要成对出现
2. 每一行指令后面要用分号结尾
3. 每一个指令要放置在指定的区块中

虚拟主机配置文件编写方法:
1. 基于域名的虚拟主机配置方法(最常用)
2. 基于端口的虚拟主机配置方法
    说明:当你访问的网站域名在虚拟主机配置中不存在时,默认会将第一个虚拟主机的配置页面响应给用户
3. 基于IP地址的虚拟主机配置方法
    说明:nginx服务中只要涉及IP地址的修改,都需要重启nginx服务,而不能采用平滑重启

实现编写一个或多个网站页面(基于域名)

#1.编写配置文件
[root@web01 html]# cat ../conf/nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  www1.etiantian.org;
        location / {
            root   html/www;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
    server {
        listen       80;
        server_name  bbs.etiantian.org;
        location / {
            root   html/bbs;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
    server {
        listen       80;
        server_name  blog.etiantian.org;
        location / {
            root   html/blog;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
#2.生成站点目录与首页文件
[root@web01 html]# for i in www bbs blog;do echo "10.0.0.7 $i.etiantian.org" > /app
lication/nginx/html/$i/index.html;done
[root@web01 html]# for i in www bbs blog;do cat  /application/nginx/html/$i/index.h
tml;done10.0.0.7
www.etiantian.org
10.0.0.7 bbs.etiantian.org
10.0.0.7 blog.etiantian.org
#3.访问测试
[root@web01 html]# curl www1.etiantian.org
10.0.0.7 www.etiantian.org
[root@web01 html]# curl bbs.etiantian.org
10.0.0.7 bbs.etiantian.org
[root@web01 html]# curl blog.etiantian.org
10.0.0.7 blog.etiantian.org
[root@web01 html]# 

实现编写一个或多个网站页面(基于端口)

#编写配置文件,更改访问端口
[root@web01 html]# cat ../conf/nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       8080;
        server_name  www1.etiantian.org;
        location / {
            root   html/www;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
    server {
        listen       8081;
        server_name  bbs.etiantian.org;
        location / {
            root   html/bbs;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
    server {
        listen       8082;
        server_name  blog.etiantian.org;
        location / {
            root   html/blog;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
#访问测试
[root@web01 html]# curl www1.etiantian.org:8080
10.0.0.7 www.etiantian.org
[root@web01 html]# curl www1.etiantian.org:8081
10.0.0.7 bbs.etiantian.org
[root@web01 html]# curl www1.etiantian.org:8082
10.0.0.7 blog.etiantian.org

实现编写一个或多个网站页面(基于IP)

#编写配置文件,因IP只有一个,所以只能一个做测试
[root@web01 html]# cat ../conf/nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  10.0.0.7;
        location / {
            root   html/www;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
#测试
[root@web01 html]# curl 10.0.0.7
10.0.0.7 www.etiantian.org

企业实用案例:每个网站目录配置文件进行分割

#主配置文件
[root@web01 conf]# cat nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    include       extra/www.conf;
    include       extra/bbs.conf;
    include       extra/blog.conf;
}
#主机配置文件
[root@web01 conf]# cat extra/www.conf 
server {
        listen       80; 
        server_name  www1.etiantian.org;
        location / { 
            root   html/www;
            index  index.html index.htm;
        }   
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }        
}
[root@web01 conf]# cat extra/bbs.conf 
server {
        listen       80; 
        server_name  bbs.etiantian.org;
        location / { 
            root   html/bbs;
            index  index.html index.htm;
        }   
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }        
}
[root@web01 conf]# cat extra/blog.conf 
server {
        listen       80; 
        server_name  blog.etiantian.org;
        location / { 
            root   html/blog;
            index  index.html index.htm;
        }   
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }        
}
#测试
[root@web01 conf]# curl www1.etiantian.org
10.0.0.7 www.etiantian.org
[root@web01 conf]# curl bbs.etiantian.org
10.0.0.7 www.etiantian.org
[root@web01 conf]# curl blog.etiantian.org
10.0.0.7 www.etiantian.org
posted @ 2019-03-11 09:29  游走在边缘的人  阅读(1032)  评论(0编辑  收藏  举报