如何重定向.php扩展名并在nginx中为所有网址添加尾部斜杠

The nginx.conf below is working, except to the home page that localdomain, redirects to localdomain/index/. This confing redirects .php to / trailing slash , and phpless url to trailing slash as well. Obviously though , something is wrong, and all the possible alterations i tried didn't work out. If i remove from the server context the rewrite ^(.*).php$ $1/ permanent; then localdomain is displayed normal without /index/ , but im loosing the .php redirection to trailing slash. Im totally confused and any solution would be greatly appreciated.

user  nginx;
worker_processes  4;
error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
pid        logs/nginx.pid;
events {
worker_connections  1024;
}
http {
    port_in_redirect off;
    server_tokens off;
    sendfile    on;
    tcp_nopush  on;
    tcp_nodelay on;
    keepalive_timeout  30;
    types_hash_bucket_size 64;
    client_max_body_size 100m;
    server_names_hash_bucket_size 128;
    include     mime.types;
    default_type  application/octet-stream;
    index  index.php index.html index.htm;
    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  logs/access.log  main;
#=====================Basic Compression=====================
    gzip on;
    #gzip_disable "msie6";
    #gzip_vary on;
    #gzip_proxied any;
    #gzip_comp_level 6;
    #gzip_buffers 16 8k;
    #gzip_http_version 1.1;
    #gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
    #gzip_static on;

server {
    listen       80;
    server_name  192.168.10.2;
    root  /home/html_public;
    index index.php index.html;
    charset UTF-8;

    rewrite ^(.*).php$ $1/ permanent;

    location @extensionless-php {
    rewrite ^(.*)/$ $1.php last;
    rewrite ^(.*)$ $1/ permanent;
    }
location / {
    try_files $uri $uri/ @extensionless-php;
    }

    #error_page  404              /404.php;
    # permanent server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
    }
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
    try_files       $uri =404;
    fastcgi_index   index.php;
    fastcgi_pass    unix:/tmp/php5-fpm.sock;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_param   SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    fastcgi_intercept_errors on;
    fastcgi_ignore_client_abort off;
    fastcgi_connect_timeout 60;
    fastcgi_send_timeout 180;
    fastcgi_read_timeout 180;
    fastcgi_buffer_size 128k;
    fastcgi_buffers 4 256k;
    fastcgi_busy_buffers_size 256k;
    fastcgi_temp_file_write_size 256k;
    include fastcgi_params;
}
location /phpMyAdmin {
    root /usr/share/;
    location ~ ^/phpMyAdmin/(.+\.php)$ {
    root        /usr/share/;
    try_files   $uri =404;
    fastcgi_index   index.php;
    fastcgi_pass    unix:/tmp/php5-fpm.sock;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include         fastcgi_params;
    }
location ~* ^/phpMyAdmin/(.+\    (jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
    root /usr/share/;
    }
    }
    location /phpmyadmin {
    rewrite ^/* /phpMyAdmin last;
    }
}
}

[Updated Version]

user  nginx;
worker_processes  4;
error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
pid        logs/nginx.pid;
events {
worker_connections  1024;
}
http {
   port_in_redirect off;
server_tokens off;
    sendfile off;
    tcp_nopush  on;
    tcp_nodelay on;
    keepalive_timeout  30;
    types_hash_bucket_size 64;
    client_max_body_size 100m;
    server_names_hash_bucket_size 128;
    include     mime.types;
    default_type  application/octet-stream;
    index  index.php index.html index.htm;
    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  logs/access.log  main;
#=====================Basic Compression=====================
    gzip on;
    #gzip_disable "msie6";
    #gzip_vary on;
    #gzip_proxied any;
    #gzip_comp_level 6;
    #gzip_buffers 16 8k;
    #gzip_http_version 1.1;
    #gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
    #gzip_static on;
server {
    listen       80;
    server_name  192.168.10.2;
    root  /home/html_public;
    charset UTF-8;
    #access_log  logs/host.access.log  main;

    rewrite ^(.*).php$ $1/ permanent;
location @extensionless-php {
    rewrite ^(.*)/$ $1.php last;
    return 301 http://$host$request_uri/;
    }
location = / { 
    rewrite ^ /index/; 
}
location / {
    try_files $uri $uri/ @extensionless-php;
    }
    #error_page  404              /404.php;
    # permanent server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
    }
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
    try_files       $uri =404;
    fastcgi_index   index.php;
    fastcgi_pass    unix:/tmp/php5-fpm.sock;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_param   SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    fastcgi_intercept_errors on;
    fastcgi_ignore_client_abort off;
    fastcgi_connect_timeout 60;
    fastcgi_send_timeout 180;
    fastcgi_read_timeout 180;
    fastcgi_buffer_size 128k;
    fastcgi_buffers 4 256k;
    fastcgi_busy_buffers_size 256k;
    fastcgi_temp_file_write_size 256k;
    include fastcgi_params;
}
location /phpMyAdmin {
    root /usr/share/;
    location ~ ^/phpMyAdmin/(.+\.php)$ {
    root        /usr/share/;
    try_files   $uri =404;
    fastcgi_index   index.php;
    fastcgi_pass    unix:/tmp/php5-fpm.sock;
    fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include         fastcgi_params;
    }
    location ~* ^/phpMyAdmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
    root /usr/share/;
    }
    }
    location /phpmyadmin {
    rewrite ^/* /phpMyAdmin last;
    }
}
}
dt20081409
中国通信 你是一个救生员,确实它有效,但现在我有同样的问题与phpmyadmin:D,它重定向到/phpMyAdmin/index/。我不会为这个项目使用数据库我工作这么大,但我将来会使用mysql,所以很高兴知道。你确定location=/{rewrite^/index/;实现这一目标是“最干净”的方法吗?
接近 5 年之前 回复
dqypcghd381390
dqypcghd381390 添加location=/{rewrite^/index/;将修复主页问题。但我认为你已经破坏了/phpMyAdmin。
接近 5 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐