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

相關文章
相關標籤/搜索