Magento Ngnix两个服务器块一个域一个IP

Magento Ngnix两个服务器块一个域一个IP,magento,nginx,server,php-7.1,magento2.2,Magento,Nginx,Server,Php 7.1,Magento2.2,我正在尝试在我的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

我正在尝试在我的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错误

不清楚应该发生什么-应该处理一台服务器与另一台服务器的正确路径是什么

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


否则,在发生404错误的情况下,应该在指定的文件中提及底层路径名(未找到),这可能会揭示出发生了什么-返回404的路径名是否确实存在于光盘上

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

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\u文件为我解决了这个问题

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

   }
  • 最好移到ServerFault

  • 虽然我不确定,但在ServerName指令中包含端口对我来说是可疑的

  • 这看起来很危险:

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


  • 如果/var/www/html/root,以及上面的目录只能由root写,那么它就可以了。否则,无论哪个用户可以写入该文件,都有可能受到根攻击。将文件复制到安全的地方,并将其包含在该位置。

    错误日志显示了什么?@TarunLalwani没有错误,我正在迎接403即使ngnix-t显示没有错误也不清楚应该发生什么:我希望服务器上有两个实例,一个通过IP访问,另一个使用本地域名。他们不应该有相同的文件。是的,它们确实存在。我正在尝试配置一个magento实例,我的域名的情况下,所有的工作都很好,但在案件的情况下,当使用IP只主页工程休息所有的网页是404我很高兴这是为你工作,但作为一个关于stackoverflow的回答,它的质量很差,因为它并没有真正启发其他用户或你自己的问题是什么。哪些配置更改对您有效?我这里有默认配置之前和之后的配置,用户可以很容易地将它们分开以查看更改,但是您只需编写一次,然后数百或数千人就会阅读它。尊重他们理解你的观点所花的时间。@mc0e包括解释:)1。我不熟悉如何移动它服务器故障,是看到许多与ngnix相关的问题张贴在这里,所以同样的2)我在ServerName中没有看到任何端口号它在listen指令中3)是的,它只能由Root用户写入