nginx-当给出IP地址而不是域名时显示不同的页面

nginx-当给出IP地址而不是域名时显示不同的页面

我已经使用 ISPConfig3 在我的 vps 上安装了 nginx。我通过 ispconfig 创建了一个域 mydomain.com,除了两件事之外,一切正常。

  • 当我访问我的 vps 的 ip 地址时,会显示我的网站。我想要显示不同的页面,例如 /var/www/index.html
  • 我想从 www.mydomain.com 重定向到 mydomain.com。

以下是已启用站点的虚拟主机:

000-应用程序.vhost

server {
        listen 8081;

        server_name _;

        root   /var/www/apps;

        client_max_body_size 20M;

        location / {
               index index.php index.html;
        }

        # serve static files directly
        location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt)$ {
               access_log        off;
        }

        location ~ \.php$ {
               try_files $uri =404;
               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_FILENAME         $request_filename;
               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   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;

               fastcgi_param   HTTPS                   $https;

               # PHP only, required if PHP was built with --enable-force-cgi-redirect
               fastcgi_param   REDIRECT_STATUS         200;
               fastcgi_pass unix:/var/lib/php5-fpm/apps.sock;
               fastcgi_index index.php;
               fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
               #fastcgi_param PATH_INFO $fastcgi_script_name;
               fastcgi_buffer_size 128k;
               fastcgi_buffers 256 4k;
               fastcgi_busy_buffers_size 256k;
               fastcgi_temp_file_write_size 256k;
        }

        location ~ /\. {
               deny  all;
        }

        location /phpmyadmin {
               root /usr/share/;
               index index.php index.html index.htm;
               location ~ ^/phpmyadmin/(.+\.php)$ {
                       try_files $uri =404;
                       root /usr/share/;
                       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_FILENAME         $request_filename;
                       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   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;

                       fastcgi_param   HTTPS                   $https;

                       # PHP only, required if PHP was built with --enable-force-cgi-redirect
                       fastcgi_param   REDIRECT_STATUS         200;
                       # To access phpMyAdmin, the default user (like www-data on Debian/Ubuntu) must be used
                       #fastcgi_pass 127.0.0.1:9000;
                       fastcgi_pass unix:/var/run/php5-fpm.sock;
                       fastcgi_index index.php;
                       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                       fastcgi_buffer_size 128k;
                       fastcgi_buffers 256 4k;
                       fastcgi_busy_buffers_size 256k;
                       fastcgi_temp_file_write_size 256k;
                       fastcgi_read_timeout 240;
               }
               location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
                       root /usr/share/;
               }
        }
        location /phpMyAdmin {
               rewrite ^/* /phpmyadmin last;
        }

        location /squirrelmail {
               root /usr/share/;
               index index.php index.html index.htm;
               location ~ ^/squirrelmail/(.+\.php)$ {
                       try_files $uri =404;
                       root /usr/share/;
                       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_FILENAME         $request_filename;
                       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   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;

                       fastcgi_param   HTTPS                   $https;

                       # PHP only, required if PHP was built with --enable-force-cgi-redirect
                       fastcgi_param   REDIRECT_STATUS         200;
                       # To access SquirrelMail, the default user (like www-data on Debian/Ubuntu) must be used
                       #fastcgi_pass 127.0.0.1:9000;
                       fastcgi_pass unix:/var/run/php5-fpm.sock;
                       fastcgi_index index.php;
                       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                       fastcgi_buffer_size 128k;
                       fastcgi_buffers 256 4k;
                       fastcgi_busy_buffers_size 256k;
                       fastcgi_temp_file_write_size 256k;
               }
               location ~* ^/squirrelmail/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
                       root /usr/share/;
               }
        }
        location /webmail {
               rewrite ^/* /squirrelmail last;
        }

        location /cgi-bin/mailman {
               root /usr/lib/;
               fastcgi_split_path_info (^/cgi-bin/mailman/[^/]*)(.*)$;
               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_FILENAME         $request_filename;
               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   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;

               fastcgi_param   HTTPS                   $https;

               # PHP only, required if PHP was built with --enable-force-cgi-redirect
               fastcgi_param   REDIRECT_STATUS         200;
               fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
               fastcgi_param PATH_INFO $fastcgi_path_info;
               fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
               fastcgi_intercept_errors on;
               fastcgi_pass unix:/var/run/fcgiwrap.socket;
        }

        location /images/mailman {
               alias /usr/share/images/mailman;
        }

        location /pipermail {
               alias /var/lib/mailman/archives/public;
               autoindex on;
        }
}

000-ispconfig.vhost

server {
        listen 8080;
        ssl on;
        ssl_certificate /usr/local/ispconfig/interface/ssl/ispserver.crt;
        ssl_certificate_key /usr/local/ispconfig/interface/ssl/ispserver.key;

        # redirect to https if accessed with http
        error_page 497 https://$host:8080$request_uri;

        server_name _;

        root   /usr/local/ispconfig/interface/web/;

        client_max_body_size 20M;

        location / {
               index index.php index.html;
        }

        # serve static files directly
        location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt)$ {
               access_log        off;
        }

        location ~ \.php$ {
               try_files $uri =404;
               include /etc/nginx/fastcgi_params;
               fastcgi_pass unix:/var/lib/php5-fpm/ispconfig.sock;
               fastcgi_index index.php;
               fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
               #fastcgi_param PATH_INFO $fastcgi_script_name;
               fastcgi_buffer_size 128k;
               fastcgi_buffers 256 4k;
               fastcgi_busy_buffers_size 256k;
               fastcgi_temp_file_write_size 256k;
        }

        location ~ /\. {
               deny  all;
        }

        location /phpmyadmin {
               root /usr/share/;
               index index.php index.html index.htm;
               location ~ ^/phpmyadmin/(.+\.php)$ {
                       try_files $uri =404;
                       root /usr/share/;
                       include /etc/nginx/fastcgi_params;
                       fastcgi_pass unix:/var/lib/php5-fpm/ispconfig.sock;
                       fastcgi_param  HTTPS on;
                       fastcgi_index index.php;
                       fastcgi_param SCRIPT_FILENAME $request_filename;
               }
               location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
                       root /usr/share/;
               }
        }
        location /phpMyAdmin {
               rewrite ^/* /phpmyadmin last;
        }

        location /squirrelmail {
               root /usr/share/;
               index index.php index.html index.htm;
               location ~ ^/squirrelmail/(.+\.php)$ {
                       try_files $uri =404;
                       root /usr/share/;
                       include /etc/nginx/fastcgi_params;
                       fastcgi_pass unix:/var/lib/php5-fpm/ispconfig.sock;
                       fastcgi_param  HTTPS on;
                       fastcgi_index index.php;
                       fastcgi_param SCRIPT_FILENAME $request_filename;
               }
               location ~* ^/squirrelmail/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
                       root /usr/share/;
               }
        }
        location /webmail {
               rewrite ^/* /squirrelmail last;
        }
}

100-mydomain.com.vhost

server {
        listen *:80;


        server_name mydomain.com www.mydomain.com;

        root   /var/www/mydomain.com/web;



        index index.html index.htm index.php index.cgi index.pl index.xhtml;



        error_log /var/log/ispconfig/httpd/mydomain.com/error.log;
        access_log /var/log/ispconfig/httpd/mydomain.com/access.log combined;

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

        location = /favicon.ico {
            log_not_found off;
            access_log off;
        }

        location = /robots.txt {
            allow all;
            log_not_found off;
            access_log off;
        }

        location /stats {

            index index.html index.php;
            auth_basic "Members Only";
            auth_basic_user_file /var/www/clients/client1/web1/web/stats/.htpasswd_stats;
        }

        location ^~ /awstats-icon {
            alias /usr/share/awstats/icon;
        }

        location ~ \.php$ {
            try_files /b4c43b69af764469f45973ffea6ff69d.htm @php;
        }

        location @php {
            try_files $uri =404;
            include /etc/nginx/fastcgi_params;
            fastcgi_pass 127.0.0.1:9010;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_intercept_errors on;
        }

}

编辑:因此,当我输入 vps 的 IP(例如 5.55.33.7)时,我希望显示某个页面,而不是托管在此 IP 上的网站。

答案1

您需要使用默认配置语句添加默认的 vhost 配置(实际上,它在新版本中已被重命名)。

相关内容