【问题标题】:Ngnix two server block one by domain another one by IPNginx 两台服务器,一台按域,另一台按 IP
【发布时间】:2018-07-19 01:27:00
【问题描述】:

我正在尝试在我的 NGnix 服务器上创建两个实例

首先将被

访问

mydomain.com(监听 80 端口)

第二次使用

172.32.32.123:81(监听81端口,此IP为服务器IP)

这是我的默认文件

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


    root /var/www/html;

    # Add index.php to the list if you are using PHP
    index index.php index.html index.htm index.nginx-debian.html;

    server_name mydomain.com;

    location / {

        try_files $uri $uri/ =404;
    }

        location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.1-fpm.sock;
    }


        location ~ /\.ht {
        deny all;
    }
}
server {
    listen       81;
    server_name  172.32.32.123:81;
    root /var/www/html/root;
   index index.html index.php;
   set $MAGE_MODE developer; # or production or developer
   set $MAGE_ROOT /var/www/html/root/;

   location / {
           try_files $uri $uri/ =404;

   }
         location ~ \.php$ {
            include snippets/fastcgi-php.conf;
    #
    #       # With php7.0-cgi alone:
    #       fastcgi_pass 127.0.0.1:9000;
    #       # With php7.0-fpm:
            fastcgi_pass unix:/run/php/php7.1-fpm.sock;
    }

    include /var/www/html/root/nginx.conf.sample;

} }

服务器块在使用域名时工作正常,但在基于 IP 的域的情况下,只有主页在内部页面上工作,我们收到 404 错误

【问题讨论】:

  • 错误日志显示什么?
  • @TarunLalwani 没有错误,我正在问候 403,即使 ngnix -t 也没有显示错误

标签: magento nginx server php-7.1 magento2.2


【解决方案1】:

目前还不清楚应该发生什么——应该处理一个服务器与另一个服务器的正确路径是什么?!

如果它们应该具有相同的底层文件,那么您的 root 指令非常可疑——一个只是 /var/www/html/,另一个是 /var/www/html/root/——这是故意的吗?

否则,如果出现 404 错误,则应在 http://nginx.org/r/error_log 指定的文件中提及底层路径名(未找到),这可能会揭示发生了什么 — 返回 404 的那些是否真的存在光盘?!

【讨论】:

  • 目前还不清楚应该发生什么:我希望我的服务器上有两个实例,一个将由 IP 访问,另一个使用本地域名。他们不应该有相同的文件。是的,它们确实存在。我正在尝试配置一个 magento 实例,我在域名的情况下一切正常,但在使用 IP 的情况下,只有主页可以正常工作,所有页面都是 404
【解决方案2】:
  1. 最好将其移至 ServerFault。

  2. 虽然我不确定,但在 ServerName 指令中包含端口对我来说看起来很可疑。

  3. 这看起来很危险:

    包括 /var/www/html/root/nginx.conf.sample

如果 /var/www/html/root 及其上面的目录只能由 root 写入,则可能没问题。否则,对于任何可以写入文件的用户来说,它都可能是 root 漏洞利用。将文件复制到安全的地方,并将其包含在该位置。

【讨论】:

  • 1.我不熟悉如何移动它服务器故障,在这里看到很多与 ngnix 相关的问题,所以也是如此 2)我在服务器名中没有看到任何端口号,它在监听指令中 3)是的,它只能由 Root 用户写入跨度>
【解决方案3】:

解决了使用以下默认配置的问题

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

    # SSL configuration
    #
    # listen 443 ssl default_server;
    # listen [::]:443 ssl default_server;
    #
    # 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;

    root /var/www/html;

    # Add index.php to the list if you are using PHP
    index index.php index.html index.htm index.nginx-debian.html;

    server_name magedev.com;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        #try_files $uri $uri/ =404;
        index index.html index.php;
                #try_files $uri $uri/  @handler;
        #try_files $uri $uri/ /index.php;
                expires 30d;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
        location ~ \.php$ {
        #include fastcgi_params;
        include snippets/fastcgi-php.conf;
    #
    #   # With php7.0-cgi alone:
    #   fastcgi_pass 127.0.0.1:9000;
    #   # With php7.0-fpm:
        fastcgi_pass unix:/run/php/php7.1-fpm.sock;
        #fastcgi_index index.php;
            #fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
        location ~ /\.ht {
        deny all;
    }
    #include /var/www/html/root/nginx.conf.sample;
}
server {
       listen 81;
       server_name 192.87.123.132;

       root /var/www/html/root;
       index index.html index.php;
     set $MAGE_MODE developer; # or production or developer
     set $MAGE_ROOT /var/www/html/root/;
# **Inclusion of try_files Solved the  issue for me**
       location / {
               try_files $uri $uri/ /index.php?$args;
               autoindex on;
               autoindex_exact_size off;

       }
             location ~ \.php$ {
                include snippets/fastcgi-php.conf;
        #
        #       # With php7.0-cgi alone:
        #       fastcgi_pass 127.0.0.1:9000;
        #       # With php7.0-fpm:
                fastcgi_pass unix:/run/php/php7.1-fpm.sock;
        }

    include /var/www/html/root/nginx.conf.sample;

}

包含 try_files 为我解决了这个问题

   location / {
           try_files $uri $uri/ /index.php?$args;
           autoindex on;
           autoindex_exact_size off;

   }

【讨论】:

  • 我很高兴这对您有用,但作为 stackoverflow 的答案,它的质量很差,因为它并不能真正启发其他用户或您自己了解问题所在。哪些配置更改对您有用?
  • 我这里有默认配置前后,用户可以轻松区分它们以查看更改
  • 是的,但是你写一次,然后成百上千的其他人阅读它。对他们理解你的观点所花费的时间表示尊重。
  • @mc0e 包括解释:)
猜你喜欢
  • 2015-03-08
  • 2010-10-24
  • 1970-01-01
  • 2018-04-24
  • 2018-01-20
  • 1970-01-01
  • 2020-11-26
  • 2014-10-10
  • 2019-02-23
相关资源
最近更新 更多