nginx多站点的配置
一、多站点配置的引入
文件/etc/nginx/nginx.conf 中引入文件夹/etc/nginx/conf.d中的站点配置文件。
引入的代码为:
include /etc/nginx/conf.d/*.conf;
二、nginx的配置文件
1、查看nginx进程
systemctl status nginx
2、做语法检测,检测你修改的配置是不是存在问题
nginx -t
3、查看nginx所用的端口
netstat -anlpt | grep nginx
4、修改nginx的默认端口
vi /etc/nginx/conf.d/default.conf
5、查看进程pid
ps aux | grep nginx
6、pid文件(每个以.pid结尾的文件里面都有一个pid,每个文件对应一个应用)
/var/run
7、查看nginx的pid
/var/run/nginx.pid
8、多站点配置文件放置的位置
文件/etc/nginx/nginx.conf 中引入文件夹/etc/nginx/conf.d中的站点配置文件。
引入的代码为:
include /etc/nginx/conf.d/*.conf;
9、查看nginx的日志文件
more /var/log/nginx/error.log
10、修改nginx配置文件后的操作
10.1、检测配置是否正确
nginx -t
10.2、重启nginx服务
systemctl reload nginx
11、查看端口
netstat -anlpt | grep nginx
三、nginx 文件结构
1、结构如下:
... #全局块
events { #events块
...}
http #http块{
... #http全局块
server #server块
{
... #server全局块
location [PATTERN] #location块
{
...
}
location [PATTERN]
{
...
}
}
server
{
...
}
... #http全局块}
2、配置文件结构说明:
1、全局块:配置影响nginx全局的指令。一般有运行nginx服务器的用户组,nginx进程pid存放路径,日志存放路径,配置文件引入,允许生成worker process数等。
2、events块(Main配置段,是核心配置段):配置影响nginx服务器或与用户的网络连接。有每个进程的最大连接数,选取哪种事件驱动模型处理连接请求,是否允许同时接受多个网路连接,开启多个网络连接序列化等。
Events {
#配置事件的
}
3、http块(标准http配置段,可选http配置段,都在http{}这里配置,每一种都是一个独立的):可以嵌套多个server,配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置。如文件引入,mime-type定义,日志自定义,是否使用sendfile传输文件,连接超时时间,单连接请求数等。
4、server块:配置虚拟主机的相关参数,一个http中可以有多个server。
5、location块:配置请求的路由,以及各种页面的处理情况。
注意:每一个配置参数都需要以分号结尾
三、多站点的配置
1、在文件/etc/nginx/conf.d中创建对应的配置文件,文件的名字可以任意取,但为了便于记忆和便于理解可以把文件名配置成相关的域名
2、有多少个站点就配置多少个配置文件
3、配置文件详细代码
server {
listen 80;#端口
server_name localhost;#域名
#charset koi8-r;//字符集
#access_log /var/log/nginx/host.access.log main;#日志
#url匹配
location / {#网站的配置,这里的斜杠代表是在下面html文件夹下
# location /hello {#网站的配置,这里的 /hello 代表是在下面html文件夹下的hello
root /usr/share/nginx/html; #这里代表的是网站的目录 网站的路径就等于你当前设置的目录下面的url
index index.html index.htm; #默认的首页
}
#error_page 404 /404.html; #找不到页面的时候显示的页面
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}