【问题标题】:How do I setup nginx for multiple web app with multiple subdomains in GCE如何在 GCE 中为具有多个子域的多个 Web 应用程序设置 nginx
【发布时间】:2021-06-20 12:15:23
【问题描述】:

我正在使用带有 LEMP 堆栈的 GCE。我有多个子域,每个子域都有不同的根文件夹。

我的设置如下:

生产应用

server {
    listen 80;
    listen [::]:80 ipv6only=on;

    # SSL configuration
    #
    listen 443 ssl http2;
    listen [::]:443 ipv6only=on ssl http2;
    #
    # Note: You should disable gzip for SSL traffic.
    # See: https://bugs.debian.org/773332
    #
    # Read up on ssl_ciphers to ensure a secure configuration.
    # See: https://bugs.debian.org/765782
    #
    # Self signed certs generated by the ssl-cert package
    # Don't use them in a production server!
    #
    # include snippets/snakeoil.conf;
    
    ssl_certificate /etc/nginx/ssl/*.example.com.crt;
    ssl_certificate_key /etc/nginx/ssl/*.example.com.key;

    server_name prod.example.com;
    root /var/www/example/web;
    client_max_body_size 10M;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        #try_files $uri $uri/ =404;
        try_files $uri /app.php$is_args$args;
    }
    
    location ~ ^/app\.php(/|$) {
            fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
            fastcgi_read_timeout 3000;

            fastcgi_split_path_info ^(.+\.php)(/.*)$;
            include fastcgi_params;
        #   When you are using symlinks to link the document root to the
        #   current version of your application, you should pass the real
        #   application path instead of the path to the symlink to PHP
        #   FPM.
        #   Otherwise, PHP's OPcache may not properly detect changes to
        #   your PHP files (see https://github.com/zendtech/ZendOptimizerPlus/issues/126
        #   for more information).
            fastcgi_param  SCRIPT_FILENAME  $realpath_root$fastcgi_script_name;
            fastcgi_param DOCUMENT_ROOT $realpath_root;
            fastcgi_buffer_size 128k;
            fastcgi_buffers 4 256k;
            fastcgi_busy_buffers_size 256k;
        #   Prevents URIs that include the front controller. This will 404:
        #   http://domain.tld/app.php/some-path
        #   Remove the internal directive to allow URIs like this
            internal;
        }

    location ~ \.php$ {
        return 404;
    }
}

暂存应用

server {
    #listen 80;
    listen [::]:80;

    # SSL configuration
    #
    #listen 443 ssl http2;
    listen [::]:443 ssl http2;
    #
    # Note: You should disable gzip for SSL traffic.
    # See: https://bugs.debian.org/773332
    #
    # Read up on ssl_ciphers to ensure a secure configuration.
    # See: https://bugs.debian.org/765782
    #
    # Self signed certs generated by the ssl-cert package
    # Don't use them in a production server!
    #
    # include snippets/snakeoil.conf;
    
    ssl_certificate /etc/nginx/ssl/*.example.com.crt;
    ssl_certificate_key /etc/nginx/ssl/*.example.com.key;

    server_name staging.example.com;
    root /var/www/example-staging/web;
    client_max_body_size 10M;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        #try_files $uri $uri/ =404;
        try_files $uri /app.php$is_args$args;
    }
    
    location ~ ^/app\.php(/|$) {
            fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
            fastcgi_read_timeout 3000;

            fastcgi_split_path_info ^(.+\.php)(/.*)$;
            include fastcgi_params;
        #   When you are using symlinks to link the document root to the
        #   current version of your application, you should pass the real
        #   application path instead of the path to the symlink to PHP
        #   FPM.
        #   Otherwise, PHP's OPcache may not properly detect changes to
        #   your PHP files (see https://github.com/zendtech/ZendOptimizerPlus/issues/126
        #   for more information).
            fastcgi_param  SCRIPT_FILENAME  $realpath_root$fastcgi_script_name;
            fastcgi_param DOCUMENT_ROOT $realpath_root;
            fastcgi_buffer_size 128k;
            fastcgi_buffers 4 256k;
            fastcgi_busy_buffers_size 256k;
        #   Prevents URIs that include the front controller. This will 404:
        #   http://domain.tld/app.php/some-path
        #   Remove the internal directive to allow URIs like this
            internal;
        }

    location ~ \.php$ {
        return 404;
    }
}

除了 server_name 部分之外,它们具有相同的配置。但是,似乎 nginx 无法区分这两种不同的配置。

无论我使用 prod.example.com 还是 staging.example.com,两者都只会路由到 prod 根文件夹。

我的配置有什么问题导致这个问题吗?

【问题讨论】:

    标签: nginx server virtual-machine google-compute-engine


    【解决方案1】:

    问题是因为我的“暂存”应用程序只监听 IPv6,我也需要打开 IPv4,所以现在不同的子域可以正确路由。

    【讨论】:

      猜你喜欢
      • 2021-03-05
      • 2021-10-01
      • 2017-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-11
      • 1970-01-01
      相关资源
      最近更新 更多