【问题标题】:How to put multiple websites on a localhost port 80 using nginx.conf file?如何使用 nginx.conf 文件将多个网站放在 localhost 端口 80 上?
【发布时间】:2023-04-01 17:33:02
【问题描述】:

我使用 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 时看到所有三个文件夹,然后从那里选择转到 @987654330 @、http://localhost/redwebhttp://localhost/greenweb。您能否查看nginx.conf 文件并将您的cmets 给我?

【问题讨论】:

    标签: nginx localhost virtualhost


    【解决方案1】:

    您想要的是使用子文件夹而不是它自己的域,这很容易配置,但在某些情况下会出现问题,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/;
      }
    }
    

    【讨论】:

    • 不幸的是它不起作用。我没有静态 html 或 php 文件。我正在使用带有 PhP 文件的框架,并且基本 URL 设置为 http://localhost。即使现在在不同的端口(80 和 100)上,我也会收到 404 错误,因为它找不到页面(例如 http://localhost/old/http://localhost/recent/ 等),可能正如您提到的那样,它将 URI 读取为 /old//recent/ 未在设置文件中定义。您能否编辑nginx.conf 文件并重新发回?谢谢
    • @Jonathan 您可以,但您需要您的 dns 服务器将这些域指向您的本地计算机,或者只需更新 /etc/hosts 并将其解析为 127.0.0.1
    猜你喜欢
    • 2010-10-21
    • 2020-05-20
    • 1970-01-01
    • 2023-03-15
    • 2015-06-18
    相关资源
    最近更新 更多