Ngnix两个服务器阻塞一个域另一个通过IP

时间:2018-02-08 13:00:31

标签: 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 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错误

3 个答案:

答案 0 :(得分:0)

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

如果他们应该拥有相同的基础文件,那么您的root指令非常可疑 - 一个只是/var/www/html/,另一个是/var/www/html/root/ - 是故意?

否则,如果出现404错误,则应在http://nginx.org/r/error_log指定的文件中提及基础路径名称(找不到),这可能会显示出最新情况 - 返回的是404确实存在于光盘上?!

答案 1 :(得分:0)

使用以下默认配置

解决了这个问题
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;

   }

答案 2 :(得分:0)

  1. 最好将其移至ServerFault。

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

  3. 这看起来很危险:

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

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

相关问题