Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

如何使用相同port同时配置多站点 #575

Closed
jiker-burce opened this issue Oct 11, 2023 · 7 comments
Closed

如何使用相同port同时配置多站点 #575

jiker-burce opened this issue Oct 11, 2023 · 7 comments

Comments

@jiker-burce
Copy link

站点1:

server {
    listen       8088  default;
    server_name  api1.website.test;
    root   /www/api1/public;

站点2:

server {
    listen       8088  default;
    server_name  api2.website.test;
    root   /www/api2/public;
  • 要求:

    • 使用相同的一级域名,相同的端口号
    • 使用不同的二级域名
  • 疑问:

    • 可以通过同一个 nginx 容器配置实现吗?如何实现呢?感谢!
@jingmian
Copy link
Collaborator

可以,按照你以上的设置不变,当然fastcgi_pass php80:9000;也不需要改动,只需要在docker-composer.yml文件中${NGINX_HTTP_HOST_PORT}:8088,改为8088:8088

@SemiThin
Copy link
Contributor

SemiThin commented Oct 11, 2023

当然可以,就这这样配置就行了,不过你这样肯定会报错的,可以把站点2的default去掉,或者两个站点的default都去掉,其他不用改

@jiker-burce
Copy link
Author

目前状况:

  • 站点1
server {
    listen       8088;
    server_name  api1.website.test;
    root   /www/api1/public;
    index  index.php index.html index.htm;

    access_log  /var/log/nginx/nginx.api1.access.log  main;
    error_log  /var/log/nginx/nginx.api1.error.log  warn;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        fastcgi_pass   php:9000; 
        include        fastcgi-php.conf;
        include        fastcgi_params;
    }
}

  • 站点2
server {
    listen       8088;
    server_name  api2.website.test;
    root   /www/api2/public;
    index  index.php index.html index.htm;

    access_log  /var/log/nginx/nginx.api2.access.log  main;
    error_log  /var/log/nginx/nginx.api2.error.log  warn;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        fastcgi_pass   php:9000; 
        include        fastcgi-php.conf;
        include        fastcgi_params;
    }
}

  • nginx
    ports:
      - "${NGINX_HTTP_HOST_PORT}:8088"
      - "${NGINX_HTTPS_HOST_PORT}:443"
  • .env
NGINX_VERSION=1.19.1-alpine
NGINX_HTTP_HOST_PORT=80
NGINX_HTTPS_HOST_PORT=443
  • 宿主机 hosts
127.0.0.1 api1.website.test
127.0.0.1 api2.website.test

  • 浏览器访问api1.website.test结果:
该网页无法正常运作 api1.website.test 目前无法处理此请求。
HTTP ERROR 502

@xiahuan710
Copy link

我觉得你先要搞秦楚nginx的基本用法,先了解什么叫空主机头,然后再来说你主机配置的问题。

@jiker-burce
Copy link
Author

我觉得你先要搞秦楚nginx的基本用法,先了解什么叫空主机头,然后再来说你主机配置的问题。

也试过了,配置空头(设置404),访问localhost 就是404,访问域名依旧是502

@jiker-burce
Copy link
Author

jiker-burce commented Oct 12, 2023

排查到问题了,是开启了vpn导致127.0.0.1 自动转发到本地的vpn端口上了。
通过观察浏览器console请求可以发现问题。

之前我的配置没啥问题;空头主机不配置也没啥关系。

@jiker-burce
Copy link
Author

经研究,解决开发需要关闭vpn的问题

  • 打开mac的网络设置
  • 找到Wi-Fi 顶部的「详细信息」
  • 找到「代理」
  • 找到「忽略这些主机与域的代理设置」
  • 添加*.test
    点击「好」

此时,打开vpn也不会出现502了。另外,感谢各位的回复。

@yeszao yeszao closed this as completed Oct 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants