重写规则不适用于 nginx

重写规则不适用于 nginx
location / {
rewrite ^/test$ /test.php last;
}

我将其添加到 /usr/local/nginx/conf/vhost.d/example.com.rewrite

也测试过

rewrite ^/test$ /test.php last;

但要https://www.example.com/test只会返回 404 错误

添加规则后,我重建了 nginx,然后重新启动

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
Reloading nginx:                                           [  OK  ]

我究竟做错了什么?

我在 CentOS 6.9 上

/usr/local/nginx/conf/vhost.ssl.d/example.com.conf

#:nginx:Nginx+ PHP-FPM Default  Stand Alone:2.0:
server {
    listen    144.217.68.82:443 ssl http2 ;
    server_name  example.com www.example.com;
    root /home/ndnmdev/public_html;
    index index.php index.php5 index.php4 index.php3 index.perl index.pl  index.cgi  index.phtml index.shtml index.xhtml index.html index.htm index.wml Default.html Default.htm default.html de$

    ssl on;
    ssl_certificate /usr/local/nginx/conf/ssl.cert.d/example.com_cert;
    ssl_certificate_key /usr/local/nginx/conf/ssl.key.d/example.com_key;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
    ssl_prefer_server_ciphers on;
    ssl_ciphers "ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AE$
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout  5m;

    #.............. Cpnginx OCSP stapling protection for security start ....................
    ssl_stapling on;
    ssl_stapling_verify on;
    ssl_trusted_certificate /usr/local/nginx/conf/ssl.ca.d/example.com_ca-bundle;
    resolver 127.0.0.1 8.8.8.8 4.2.2.1 8.8.4.4 4.2.2.2  valid=300s;
    resolver_timeout 5s;
    #.............. Cpnginx OCSP stapling protection for security end....................

    location = /favicon.ico {
        log_not_found off;
    }


    access_log /usr/local/apache/domlogs/example.com-bytes_log bytes_log buffer=32k flush=5m;
    access_log /usr/local/apache/domlogs/example.com-ssl_log combined buffer=32k flush=5m;

    referer_hash_bucket_size 512;
    # Run Staic  file directly from nginx
    location ~* ^.+.(jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|iso|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|mp3|ogv|ogg|flv|swf|mpeg|mpg|mpeg4|mp4|avi|wmv|js|css|3gp|sis|sisx|nth|svg)$ {
        expires 30d;
        add_header Pragma public;
        add_header Cache-Control "public, must-revalidate, proxy-revalidate";
    }
    keepalive_requests 100;
    keepalive_timeout 60s;
    autoindex on;
    # Disable direct access to .ht files and folders
    location ~ /\.ht {
          deny all;
    }
    # Access all ****** services
    location ~* ^/(******|webmail|***|bandwidth|img-sys|java-sys|mailman/archives|pipermail|sys_******|cgi-sys|mailman) {
        proxy_pass   http://144.217.68.82:9080;
        proxy_set_header   Host   $host;
        proxy_set_header   X-Real-IP  $remote_addr;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
    }







    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/opt/******/ea-php70/root/usr/var/run/php-fpm/ndnmdev.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include /usr/local/nginx/conf/fastcgi_params;

        }

    # Allow Let's Encrypt client authentication - letsencrypt.org RFC 5785
    location ~ /.well-known { allow all; }

    location / {
        include /usr/local/nginx/conf/vhost.ssl.d/example.com.rewrite;
        client_max_body_size    2000m;
        client_body_buffer_size 512k;
        try_files $uri $uri/ /index.php?$args;


    }
    # include /usr/local/nginx/conf/vhost.ssl.d/example.com.include;

}

server {
    listen    144.217.68.82:443 ssl http2 ;
    server_name ******.example.com ***.example.com webmail.example.com webdisk.example.com cpcalendars.example.com cpcontacts.ni-dieu-ni-$
    ssl on;
    ssl_certificate /usr/local/nginx/conf/ssl.cert.d/example.com_cert;
    ssl_certificate_key /usr/local/nginx/conf/ssl.key.d/example.com_key;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout  5m;
    access_log off;
    location / {
            location ~ /.well-known{
                root /home/ndnmdev/public_html;
            }
        proxy_pass   https://127.0.0.1:9443;
        proxy_set_header   Host   $host;
        proxy_set_header   X-Real-IP  $remote_addr;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

答案1

我怀疑正则表达式是有效的,但它没有给出你期望的结果,这取决于平台。如果你写

rewrite ^/test$ http://www.example.com/test.php last;

它会起作用。请检查您的访问和错误日​​志,看看您的语法指向哪里。请不要添加第二个块位置。

相关内容