Nginx 阻止问题:所有网站都重定向到默认网站

Nginx 阻止问题:所有网站都重定向到默认网站

我有一个 Nginx 服务器正在运行,为单个站点提供服务,现在我已将 Nginx 设置为使用块,以便我可以为多个站点提供服务。问题是所有站点都会重定向到默认站点。我在 中设置了两个站点条目etc/hosts

user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log          off;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    gzip             on;
    gzip_comp_level  2;
    gzip_min_length  256;
    gzip_proxied     any;
    gzip_vary        on;
    gzip_types
        application/javascript
        application/json
        application/rss+xml
        application/xml
        text/css
        text/plain;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    include /etc/nginx/sites-enabled/*.conf;
    server_names_hash_bucket_size 64;
}

我的网站看起来像这样,唯一的区别是rootdefault_server在一个网站上设置的值server_name显然不同。

ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

server {
    listen          80;
    listen          [::]:80;
    server_name     www.example.com example.com;
    server_tokens   off;

    return          301 https://www.example.com$request_uri;
}

server {
    listen          443 ssl http2;
    listen          [::]:443 ssl http2;
    server_name     example.com;
    server_tokens   off;

    return          301 https://www.example.com$request_uri;
}

server {
    listen          443 ssl http2;
    listen          [::]:443 ssl http2;
    server_name     www.example.com;
    server_tokens   off;

    root            /var/www/example.com/;
    index           index.html index.php
    rewrite_log     on;

    try_files $uri $uri/ =404;

    rewrite ^([^.]*[^/])$ $1/ permanent;

    location ~* \.php$ {
        fastcgi_pass            unix:/var/run/php-fpm/php7.1-fpm.sock;
        fastcgi_index           index.php;
        include                 fastcgi_params;
        fastcgi_read_timeout    60;
        fastcgi_param           SCRIPT_FILENAME    $document_root$fastcgi_script_name;
        fastcgi_param           SCRIPT_NAME        $fastcgi_script_name;
        try_files $uri = 404;
    }

    location ~ /\. {
        access_log      off;
        log_not_found   off;
        deny            all;
    }

    ## Cache files that don't change that often
    location ~* .(jpg|jpeg|gif|ico|css|js|png)$ {
        expires 7d;
    }

    ## Block unsupported file extensions
    location ~ \.(aspx|asp|html|htm|jsp|cgi)$ {
        return 404;
    }

    ###################
    ## Rewrite Rules
    ###################
    location / {
        rewrite ^/(.*)\.htm$ /$1.php;
    }
}

如果有人有任何建议,我将不胜感激,我尝试了所有我能想到的。

相关内容