3

如何安装nginx,并根据不同的项目读取不同的配置文件

 2 years ago
source link: https://blog.51cto.com/u_12386780/5066629
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

如何安装nginx,并根据不同的项目读取不同的配置文件

原创

公号运维家 2022-03-03 09:55:01 博主文章分类:linux ©著作权

文章标签 nginx html 配置文件 文章分类 Linux 系统/运维 阅读数1475

​声明:本文乃“运维家”原创,转载请注明出处,更多内容请关注公众号“运维家”。​

如何安装nginx,并根据不同的项目读取不同的配置文件_html

​主旨​

在我们使用nginx的过程中,由于他本身性能极好,所以往往对应着不同的项目,且不同的项目配置也不尽相同,这种情况下如果每个项目都对应着自己的配置文件的话,会有很强的阅读性和极高的易维护度。

​环境​

linux环境
nginx环境

PS:nginx如何安装的话,可以关注公众号“运维家”,后台回复“106”进行查看。

​配置初衷​

有三个项目,分别是test_a,test_b,test_c,想使用三个不同的配置文件来代理这三个项目,在这里的话不同的项目,使用不同的html代替了,可以理解哈,使用端口映射如下:

test_a:10000
test_b:10001
test_c:10002

需要说明一点的是,每个配置文件里面都可以映射多个端口,并不是说只能映射一个端口。

创建代替项目的三个html文件:

[yunweijia@localhost nginx]$ pwd
/usr/local/nginx
[yunweijia@localhost nginx]$ cd html/
[yunweijia@localhost html]$ echo 'my name is test_a' > test_a.html
[yunweijia@localhost html]$ echo 'my name is test_b' > test_b.html
[yunweijia@localhost html]$ echo 'my name is test_c' > test_c.html
[yunweijia@localhost html]$ ls
test_a.html test_b.html test_c.html
[yunweijia@localhost html]$

​开始配置​

在http中添加include参数,并指定配置文件存放路径,意思是加载该路径下的配置文件。

[yunweijia@localhost nginx]$ pwd
/usr/local/nginx
[yunweijia@localhost nginx]$ vim conf/nginx.conf
# 在http{}里面添加如下信息
include /usr/local/nginx/conf.d/*.conf;
[yunweijia@localhost nginx]$

上面配置的意思是,nginx程序在启动的时候自动去加载“/usr/local/nginx/conf.d”目录下以“.conf”结尾的配置文件。

在指定目录下新建我们的项目配置文件,如下:

[yunweijia@localhost nginx]$ mkdir conf.d
[yunweijia@localhost nginx]$ cd conf.d/
[yunweijia@localhost conf.d]$ touch {test_a,test_b,test_c}.conf
[yunweijia@localhost conf.d]$ ls
test_a.conf test_b.conf test_c.conf
[yunweijia@localhost conf.d]$

新建的项目配置文件中书写nginx配置的时候,直接从server开始书写,例子如下:

剩余内容请关注微信公众号 “运维家” ,回复 “108” 查看。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK