博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Nginx配置文件编写实例
阅读量:7039 次
发布时间:2019-06-28

本文共 5485 字,大约阅读时间需要 18 分钟。

编写nginx服务配置文件

nginx配置文件三个语法格式说明:1. 大括号要成对出现2. 每一行指令后面要用分号结尾3. 每一个指令要放置在指定的区块中虚拟主机配置文件编写方法:1. 基于域名的虚拟主机配置方法(最常用)2. 基于端口的虚拟主机配置方法    说明:当你访问的网站域名在虚拟主机配置中不存在时,默认会将第一个虚拟主机的配置页面响应给用户3. 基于IP地址的虚拟主机配置方法    说明:nginx服务中只要涉及IP地址的修改,都需要重启nginx服务,而不能采用平滑重启

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

#1.编写配置文件[root@web01 html]# cat ../conf/nginx.confworker_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" > /application/nginx/html/$i/index.html;done[root@web01 html]# for i in www bbs blog;do cat  /application/nginx/html/$i/index.html;done10.0.0.7www.etiantian.org10.0.0.7 bbs.etiantian.org10.0.0.7 blog.etiantian.org#3.访问测试[root@web01 html]# curl www1.etiantian.org10.0.0.7 www.etiantian.org[root@web01 html]# curl bbs.etiantian.org10.0.0.7 bbs.etiantian.org[root@web01 html]# curl blog.etiantian.org10.0.0.7 blog.etiantian.org[root@web01 html]#

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

#编写配置文件,更改访问端口[root@web01 html]# cat ../conf/nginx.confworker_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:808010.0.0.7 www.etiantian.org[root@web01 html]# curl www1.etiantian.org:808110.0.0.7 bbs.etiantian.org[root@web01 html]# curl www1.etiantian.org:808210.0.0.7 blog.etiantian.org

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

#编写配置文件,因IP只有一个,所以只能一个做测试[root@web01 html]# cat ../conf/nginx.confworker_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.710.0.0.7 www.etiantian.org

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

#主配置文件[root@web01 conf]# cat nginx.confworker_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.org10.0.0.7 www.etiantian.org[root@web01 conf]# curl bbs.etiantian.org10.0.0.7 www.etiantian.org[root@web01 conf]# curl blog.etiantian.org10.0.0.7 www.etiantian.org

转载于:https://www.cnblogs.com/yjiu1990/p/10508682.html

你可能感兴趣的文章
paper 135:关于C#泛型的知识点
查看>>
第二十四条:消除非受检警告
查看>>
给阅读的网页作标记
查看>>
vue条件渲染
查看>>
转 MySQL数据库基础
查看>>
Oracle dblink创建
查看>>
python04 while循环
查看>>
web 开发之酷炫--- 酷炫展示
查看>>
ubuntu 解压命令全部
查看>>
Chrome教程(一)NetWork面板分析网络请求
查看>>
第十八回  基础才是重中之重~开发人员应学会用throw
查看>>
Rosenblatt's perceptron
查看>>
1570:基础练习 分解质因数
查看>>
判断ie浏览器7、8、9三个版本
查看>>
GDUFE ACM-1124
查看>>
Schwarz积分公式
查看>>
工作中常用的 Linux 命令
查看>>
English Corner
查看>>
(最短路 SPFA)Invitation Cards -- poj -- 1511
查看>>
两数相加LeetCode
查看>>