nginx设置绑定解析实现二级域名多域名

 

apache(httpd)配置多个二级域名看这个连接:http://www.javashuo.com/article/p-wyxhjvba-gn.htmlphp

 

网站的目录结构为
/home/www
├── bbs.yourdomain.com
└── www.yourdomain.comhtml

html为nginx的安装目录下默认的存放源代码的路径。nginx

bbs为论坛程序源代码路径
www为主页程序源代码路径apache

把相应程序放入上面的路径经过
http://www.youdomain.com 访问的就是主页
http://bbs.yourdomain.com 访问的就是论坛
其它二级域名类推。服务器

 

前言:session

如今不少人都会解析www二级域名做为主网站。可想弄个博客的网站呢,条件又只有一个ip一台服务器,能够使用nginx设置多个域名。app

 

注意:nginx默认的配置文件是nginx.confdom

 

[root@localhost ~]# find / -name nginx.conf  ##查找这个默认配置文件目录
/etc/nginx/nginx.conf
[root@localhost ~]# 

Nginx加载的就是这个配置文件内的内容,下面附nginx.conf的内容tcp

user  nginx;
worker_processes  auto;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;
worker_rlimit_nofile 655350;

events {
    worker_connections  102400;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  logs/access.log  main;

    sendfile        on;
    tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /home/www/html/;
            index index.php index.html index.htm;
            if (!-e $request_filename) {
                rewrite ^(.*)$ /index.php?s=$1 last;
            }
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        #error_page   500 502 503 504  /50x.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 /home/www/html/;
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;
                fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
                fastcgi_param PATH_INFO $fastcgi_path_info;
                fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
                fastcgi_param SCRIPT_FILENAME $document_root$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;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}
nginx.conf

 

须要配置多个域名为方便管理。我指定这个nginx.conf加载指定配置文件*.confide

下面附我修改后的nginx.conf,记得先cp nginx.conf nginx.old.conf备份

user  nginx;
worker_processes  auto;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;
worker_rlimit_nofile 655350;

events {
    worker_connections  102400;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  logs/access.log  main;

    sendfile        on;
    tcp_nopush     on;

    keepalive_timeout  65;

    gzip  on;


  # server {
#      listen 80 default;
 #      server_name _;
  #     server_name ~.*;
#       return 403;
#    }


include /etc/nginx/conf.d/*.conf;   ###加载这个位置的.conf文件
}
nginx.conf

其实就是删除了默认一些配置,让这个配置文件指定加载我指定文件夹的*.conf

目录/etc/nginx/conf.d下

新建一个配置文件

vi /etc/nginx/conf.d/default.conf

 

下面附default.conf的配置文件内容

server{
listen             80;                    #监听的端口号
server_name     www.youdomain.com;    #您的域名
      location / {
          root /home/www/www.youdomain.com;        #站点的路径
          index index.php index.html index.htm;
          if (!-e $request_filename) {
              rewrite ^(.*)$ /index.php$1 last;
          }
      }
location ~ ^.+\.php {
          root /home/www.youdomain.com;        #站点的路径
          fastcgi_pass 127.0.0.1:9000;    #根据本身的 php-fpm 配置填写
          fastcgi_index index.php;
          ###配置支持pathinfo
          fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
            fastcgi_param PATH_INFO $fastcgi_path_info;
            fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
          fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
          include fastcgi_params;
}
}


server{
listen             80;                    #监听的端口号
server_name     bbs.yourdomain.com;    #您的域名
      location / {
          root /home/www/bbs.yourdomain.com;        #站点的路径
          index index.php index.html index.htm;
          if (!-e $request_filename) {
              rewrite ^(.*)$ /index.php$1 last;
          }
      }
location ~ ^.+\.php {
          root /home/www/bbs.yourdomain.com;        #站点的路径
          fastcgi_pass 127.0.0.1:9000;    #根据本身的 php-fpm 配置填写
          fastcgi_index index.php;
          ###配置支持pathinfo
          fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
            fastcgi_param PATH_INFO $fastcgi_path_info;
            fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
          fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
          include fastcgi_params;
}
}
default.conf

 

保存退出后重启nginx便可

systemctl restart nginx.service

 

总结一下步骤就是

1.确认要增长的二级域名,如bbs.yourdomain.com换成你的域名后添加到你的default.conf配置文件

2.设置bbs.yourdomain.com解析到你的nginx服务器ip ###这个须要到你的域名供应商那里操做,就和解析www同样解析这个自定义的bbs。

4.在网站目录下(我这里是、home/www)建立bbs.yourdomain.com目录

5.把源码放入bbs.yourdomain.com目录

6.从新加载nginx配置

7.访问http://bbs.yourdomain.com