【问题标题】:How to make 2 servers on nginx. One just accessible by IP and another one just by localhost如何在 nginx 上创建 2 个服务器。一个只能通过 IP 访问,另一个只能通过 localhost
【发布时间】:2021-01-08 01:38:51
【问题描述】:

我的服务器上有 2 个包含不同站点的文件夹

一个是 /var/www/html 我希望我的 IP 可以访问它:80 另一个是 denny

一个是 /var/www/cats 我希望我的 localhost:80 可以访问它,另一个是 denny

如何在我的 nginx 设置中找出它?

UPD。 1

我有 2 个配置

sudo nano /etc/nginx/sites-enabled/default
server {
        listen 122.111.111.40:80;
        root /var/www/html/;
        index /;
        server_name 122.111.111.40;
        location / {
                allow 122.111.111.40;
                deny all;
                autoindex on;
                index index.php;
                try_files $uri /index.html /index.php;
        }
        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        }
        location ~ /\.ht {
                deny all;
        }
}



sudo nano /etc/nginx/sites-enabled/cats
server {
        listen   127.0.0.1:80;
        root /var/www/cats/;
        index /index.php;
        server_name localhost;
        location / {
                allow 127.0.0.1;
                deny all;
                autoindex on;
                index index.php;
                try_files $uri /index.html /index.php;
        }
        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        }
        location ~ /\.ht {
                deny all;
        }
}

当我访问 localhost 时没关系。我有通往cats/index.php 文件夹的路线工作正常。当我路由到我的 IP 时,我有 403 Forbidden 它说

2020/09/22 05:44:02 [error] 17563#17563: *853 access forbidden by rule, client: 115.111.11.111, server: localhost, request: "GET / HTTP/1.1", host: "122.111.111.40".

所以我的配置有问题。但我无法理解是哪一个。如果我请求我的IP/index.php,我将看到cats/index.php 的索引页,但应该是html/index.php。对吧?

UPD。 2

添加 sudo nano /etc/nginx/sn-ps/fastcgi-php.conf

# regex to split $uri to $fastcgi_script_name and $fastcgi_path
fastcgi_split_path_info ^(.+\.php)(/.+)$;

# Check that the PHP script exists before passing it
try_files $fastcgi_script_name =404;

# Bypass the fact that try_files resets $fastcgi_path_info
# see: http://trac.nginx.org/nginx/ticket/321
set $path_info $fastcgi_path_info;
fastcgi_param PATH_INFO $path_info;

fastcgi_index index.php;
include fastcgi.conf;

UPD。 3

添加 sudo nano /etc/nginx/fastcgi.conf

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  REQUEST_SCHEME     $scheme;
fastcgi_param  HTTPS              $https if_not_empty;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;

【问题讨论】:

    标签: nginx


    【解决方案1】:

    假设您没有任何其他网站,以下配置应该可以工作:

    server {
        listen <ip>:80;
        root /var/www/html;
        ...
    }
    server {
        listen 127.0.0.1:80;
        root /var/www/cats;
        ...
    }
    

    【讨论】:

    • 因为作者想要拒绝其他类型的请求,所以他应该添加另一个服务器块来捕获这些请求并拒绝它们。否则,这些请求将被上述两个服务器块之一捕获。
    • @TomNguyen 我的示例将在 处使用第一个 server 块提供任何请求,在 127.0.0.1 处使用第二个请求提供第二个请求,无论 HTTP Host 标头值如何。如果 OPs 服务器是具有其他一些 IP 地址的多宿主系统,则此配置将不会为该地址上的请求提供服务。如果他想通过 HTTP Host 值过滤服务请求,here 我就这个问题给出了答案(尽管在 OPs 问题中没有提到)。
    • @ArthurYakovlev 请将文件snippets/fastcgi-php.conf 的内容也包含在您的问题中。该文件可能有问题。否则,请求IP/index.php 不会得到cats/index.php 的结果。无论如何,您的配置有一些问题。一旦我知道文件 snippets/fastcgi-php.conf 的内容,我会修复它们
    • @ArthurYakovlev,我看不出文件snippets/fastcgi-php.conf 有任何问题。但是,它包括fastcgi.conf。因此,请将fastcgi.conf 文件的内容也包含在您的问题中。
    猜你喜欢
    • 2013-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-05
    相关资源
    最近更新 更多