全球主机交流论坛

标题: 小白 问个 NGINX 配置子域名问题 [打印本页]

作者: whk0425    时间: 2018-9-13 22:22
标题: 小白 问个 NGINX 配置子域名问题
需求: 同一IP, 80端口,多个子域名访问

         a.xxx.com 指向目录 var/www/html/a.xxx.com
         b.xxx.com 指向目录 var/www/html/b.xxx.com

我的做法是 修改 etc/nginx/sites-avaliable/default  如下

                       
server {
        listen 80 ;
        listen [::]:80 ;
        server_name a.xxx.com ;
       
        root /var/www/html/a.xxx.com;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html index.php;

       

        location / {
        index index.html index.htm index.php default.html default.htm default.php;
        }
        location ~ .*\.php(\/.*)*$ {
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;

        #fastcgi_pass 127.0.0.1:9000;

        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
       
        }
       
}





server {
        listen 80 ;
        listen [::]:80 ;
        server_name b.xxx.com ;
       
        root /var/www/html/b.xxx.com;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html index.php;

       

        location / {
        index index.html index.htm index.php default.html default.htm default.php;
        }
        location ~ .*\.php(\/.*)*$ {
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;

        #fastcgi_pass 127.0.0.1:9000;

        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
       
        }
       
}


问题是 nginx 无法成功重启,有错误,将第二个 80端口 改成其他可以正常启动,但是域名访问失效,只能通过端口来区分访问目录,在网上搜了一大把,没有一个靠谱的,这应该是最基础的了吧,往大神指点一二。不甚感激,搞得头大!

作者: JamCh01    时间: 2018-9-14 09:09
试一试分开写
/etc/nginx/sites-avaliable/a
/etc/nginx/sites-avaliable/b
然后分别软连接到/etc/nginx/sites-enable
作者: whk0425    时间: 2018-9-14 09:15
好的,我回去试试。谢谢大佬
作者: whk0425    时间: 2018-9-14 10:58
试了下,不成功。
作者: qytang    时间: 2018-9-14 17:40
在第一个server的80端口后面加上reuse,具体的可以查看手册
作者: whk0425    时间: 2018-9-14 17:51
server {
        listen 80  reuse ;
        listen [::]:80 ;

结果:
Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.
作者: qytang    时间: 2018-9-14 18:13
whk0425 发表于 2018-9-14 17:51
server {
        listen 80  reuse ;
        listen [::]:80 ;


是 reuseport,记错了,不是reuse。。。
我一般会加一个default server,其他的server正常写,可以省好多麻烦
  1. server {
  2.     listen          80 fastopen=3 reuseport default;
  3.     listen          [::]:80  fastopen=3 reuseport default;
  4.     server_name    _;
  5. }
复制代码

作者: whk0425    时间: 2018-9-14 18:25
qytang 发表于 2018-9-14 18:13
是 reuseport,记错了,不是reuse。。。
我一般会加一个default server,其他的server正常写,可以省好多 ...

谢谢大佬,搞定了,感激涕零!!!!




欢迎光临 全球主机交流论坛 (https://fd.vvwvv.eu.org/) Powered by Discuz! X3.4