nginx配置server模块的问题
问题描述
假设有5个二级域名:
aaa.example.combbb.example.comccc.example.comddd.example.comeee.example.com
在配置nginx的时候,server模块是这样的:
server { listen 443 ssl http2;server_name aaa.example.com;root /var/www/aaa.example.com/public; index index.php index.html index.htm;location / {root /var/www/aaa.example.com/public;try_files $uri $uri/ /index.php?$query_string;index index.php index.html index.htm; } location ~ .php$ {try_files $uri /index.php =404;fastcgi_split_path_info ^(.+.php)(/.+)$;fastcgi_pass unix:/dev/shm/php-fpm.sock;fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;includefastcgi_params; }#... #... #...}
问题:1、有5个二级域名,必须要写5个server模块吗?可以写个通用的只用一个server模块吗?
2、如果写5个server模块的话,每个server模块中的location ~ .php${ }模块是一样的,这个location ~ .php${ }模块可以只写一遍来共用吗?也就是可以把它弄到server模块的上一层模块http模块去吗?
3、看到很多示例的root和index都要写两遍,server里面写一遍,下一层的location / { }模块中再写一遍,这是为什么?
问题解答
回答1:server_name ~^(?<site>(aa|bb|cc).example.com)$;root /var/www/$site/public; # location 里的root如果一样,可以不需要了,index也一样,大多数人是复制的回答2:
1.当你的5个域名指向同一根目录,表示同一站点时,server_name可以指定多个域名,用空格分隔;当你的5个域名表示不同站点,就要配置多个server段,通常用include指令来引入多个conf文件,每个域名是一个conf文件。2.location 指令只能是在server、location里;详见官方文档说明:
3.location里的root index 可以共用server里的root index.
server name是可以指定多个域名的,用空格分隔
相关文章:
1. macos - mac下docker如何设置代理2. javascript - Js对象怎么通过value值拿到key值?3. javascript - [MUI 子webview定位]4. vue.js - Vue APP基于webpack的项目,它是独立的项目吗?我后台是Java的,要如何实现,跨域请求吗?大牛请教一下谢谢5. 前端 - 怎样让scale缩小的元素不占据原来的空间?6. docker - 各位电脑上有多少个容器啊?容器一多,自己都搞混了,咋办呢?7. dockerfile - 为什么docker容器启动不了?8. docker 下面创建的IMAGE 他们的 ID 一样?这个是怎么回事????9. java - SSH 根据id字段不能更新数据问题10. docker-compose 为何找不到配置文件?
![javascript - [MUI 子webview定位]](http://www.haobala.com/attached/image/news/202302/105404965f.jpg)
网公网安备