nginx配置一台服务器两个域名

上一篇博客搭建了bind9环境,使虚拟机中的ubuntu作为一个dns服务器,作为测试环境,虚拟机中的另一个windows系统用域名访问是可行的,如果突然ping不通了,可能要手动修改/etc/resolv.conf,配置nameserver,虽然麻烦,测试也勉强够用了

接下来配置nginx使其两个域名访问不同的目录

已配置域名:www.testj.com和www.testk.com

nginx配置如下:

nginx.conf的http模块的最后一行添加,ubuntu的nginx.conf路径是/usr/local/nginx/conf/nginx.conf

include vhost/*.conf;

这句话的意思是包含/usr/local/nginx/conf/vhost/目录下的所有.conf文件,然后就可以每个域名写一个.conf

vhost目录下新建default.conf内容如下

    server {
        listen       80;
	#server_name 192.168.235.129;
	server_name testj.com www.testj.com;
	root  /home/wwwroot/default/Project/public;

        #access_log  logs/host.access.log  main;

        #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   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/wwwroot/default/Project/public;
            fastcgi_pass   unix:/tmp/php-cgi.sock;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

		location  / {
            index  index.html index.htm;
            add_header X-Location /;
            try_files $uri /index.php$is_args$args;
            fastcgi_pass   unix:/tmp/php-cgi.sock;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            #include fastcgi.conf;
            include fastcgi_params;
        }


        location ^~ /commonweb/ {
            root /home/wwwroot/default;
            try_files $uri $uri/ /debug/a/index.html;
            index index.html index.htm;
        }
    }

这是纯php搭建的根目录环境

接下来是tp5框架的环境,vhost目录下新建tp5.conf如下

    	server {
        	listen       80;
		    server_name testk.com www.testk.com;
		    root  /home/wwwroot/default;

            location ~ \.php{
                root           /home/wwwroot/default/tp5/public/;
                fastcgi_pass   unix:/tmp/php-cgi.sock;
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                include        fastcgi_params;
            }

            location / {
                root /home/wwwroot/default;
                index  index.html index.htm index.php;

                if (!-e $request_filename) {
                    rewrite  ^(.*)$  /index.php?s=/$1  last;
                    break;
                }
            }


        }

linux环境下编写的配置文件,可能有2、4、8个空格,还有有tab,所以可能不对齐。

然后在另一个虚拟系统windows中打开浏览器访问了公共目录下的项目,调用http请求,可以从不同的域名请求下拿到不同的数据了

猜你喜欢

转载自blog.csdn.net/youyudexiaowangzi/article/details/87939987