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 错误

4

3 回答 3

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;

   }
于 2018-02-12T10:15:00.293 回答
0
  1. This would be better moved to ServerFault.

  2. While I'm not sure of it, The inclusion of the port in the ServerName directive looks suspicious to me.

  3. This looks dangerous:

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

If /var/www/html/root, and the directories above it are only writeable by root, it might be OK. Otherwise it's likely to be a root exploit for whichever user can write to the file. Copy the file to somewhere safe, and include it at that location.

于 2018-02-19T02:57:54.437 回答
0

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

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

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

于 2018-02-12T06:39:04.657 回答