nginx配置访问域名返回404页面,其他页面反代到upstream

 直接上nginx配置文件

upstream whz_t_p {
    server 192.168.1.135:80 weight=1 max_fails=1 fail_timeout=30s;
}

server {
	listen 80;
	server_name wanghz.hw801.com;
	server_name_in_redirect off;
	log_not_found on;
	if ($scheme != https) {
            rewrite ^(.+)$ https://$host$1 permanent;
        }
}

server {
        listen 443 ssl;
        server_name wanghz.hw801.com;
        server_name_in_redirect off;
        if ($scheme != https) {
                rewrite ^(.+)$ https://$host$1 permanent;
        }
        #ssl                    on;
        ssl_certificate         certs/wanghz.hw801.com.crt;
        ssl_certificate_key     certs/wanghz.hw801.com.key;
        ssl_protocols           TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers             "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA:ECDHE-RSA-
DES-CBC3-SHA:AES256-GCM-SHA384:AES128-GCM-SHA256:AES256-SHA256:AES128-SHA256:AES256-SHA:AES128-SHA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!MD5:!PSK:!RC4";
        #ssl_prefer_server_ciphers      on;
        ssl_session_cache               shared:SSL:10m;
        ssl_session_timeout             10m;
        log_not_found on;
        error_page 404 /404.html;
        location = /404.html {
                root /home/web/hw801/whz;
        }
        location = / {
            return 404;
        }

        location / {
             proxy_pass http://whz_t_p;
             proxy_set_header Host $host:$server_port;
             proxy_set_header X-Real-IP $remote_addr;
             proxy_set_header X-Real-PORT $remote_port;
             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
             proxy_connect_timeout 300;
             proxy_send_timeout 300;
             proxy_read_timeout 600;
             proxy_buffers 4 256k;
             proxy_busy_buffers_size 256k;
             proxy_temp_file_write_size 256k;
             proxy_next_upstream error timeout invalid_header http_502 http_503 http_504;
             proxy_max_temp_file_size 128m;
        }
}

 有几点需要注意的地方:

1、upstream反向代理配置

upstream whz_t_p {
    server 192.168.1.135:80 weight=1 max_fails=1 fail_timeout=30s;
}

2、强制https

if ($scheme != https) {
    rewrite ^(.+)$ https://$host$1 permanent;
}

3、指定404页面

error_page 404 /404.html;
location = /404.html {
    root /home/web/hw801/whz;
 }

4、根据localtion优先级配置 / 显示404页面,其他全部正常

location = / {
     return 404;
}

location / {
     proxy_pass http://whz_t_p;
     ...........  
}

  

posted @ 2020-12-24 18:15  MR__Wang  阅读(2327)  评论(0编辑  收藏  举报