如何使用nginx.conf文件将多个网站放在localhost端口80上?

时间:2014-03-10 00:39:49

标签: nginx localhost virtualhost

我使用nginx设置虚拟服务器并拥有如下所示的nginx.conf文件,该文件适用于http://localhosthttp://localhost:100上的两个不同网站:

user  nobody;
worker_processes  1;
error_log /usr/local/Cellar/nginx/1.4.6/logs/error.log;
pid       /usr/local/Cellar/nginx/1.4.6/logs/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /usr/local/etc/nginx/mime.types;
    include       /usr/local/etc/nginx/fastcgi.conf;
    default_type  application/octet-stream;
    access_log    /usr/local/var/log/nginx/access.log;

    sendfile        on;
    tcp_nopush     on;
    keepalive_timeout  65;
    gzip  on;
    server {
        listen       80;
        server_name  localhost;
        access_log  /usr/local/Cellar/nginx/1.4.6/logs/localhost.access.log  combined;

        location / {
            root   /Users/apiah/Websites/greenapple;
            index  index.html index.htm index.php;
        }
        location ~ \.php$ {
            root           /Users/apiah/Websites/greenapple;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param SCRIPT_FILENAME /Users/apiah/Websites/greenapple$fastcgi_script_name;
            include        /usr/local/etc/nginx/fastcgi_params;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
    server {
        listen       100;
        server_name  localhost;
        access_log  /usr/local/Cellar/nginx/1.4.6/logs/localhost.access.log  combined;

        location / {
            root   /Users/apiah/Websites/blueweb;
            index  index.html index.htm index.php;
        }
        location ~ \.php$ {
            root           /Users/apiah/Websites/blueweb;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param SCRIPT_FILENAME /Users/apiah/Websites/blueweb$fastcgi_script_name;
            include        /usr/local/etc/nginx/fastcgi_params;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }    
}

我想在同一个端口80 http://localhost上测试上述两个(或更多)网站。例如,假设我们有三个名为bluewebredwebgreenweb的文件夹,因此我希望能够在转到http://localhost然后再查看所有三个文件夹从那里选择转到http://localhost/bluewebhttp://localhost/redwebhttp://localhost/greenweb。您能否查看nginx.conf文件并给我您的意见?

1 个答案:

答案 0 :(得分:0)

你想要的是使用子文件夹而不是自己的域,它很容易配置,但在某些情况下会出现问题,URI将以网站名称为前缀。

所以,如果您使用的只是静态html页面或直接php文件,那就没问题,但是如果您使用类似框架的东西并且该框架正在使用URI进行路由,那么那里会有一些问题,因为路线不匹配。

例如,如果您访问http://localhost/redweb/homepage,URI将是/redweb/homepage,但网站应该看到的实际URI是/homepage,要修复它,您需要为每个创建重写网站。

你可以尝试这个,如果它不起作用,那么告诉我,我会尽力帮助你。

server {
  listen 80;
  server_name localhost;
  root /my/web/root;
  index index.html index.php;
  location / {
    try_files $uri $uri/;
  }
}