dongxie45083 2015-12-05 07:01
浏览 133

如何重定向.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;
    }
}
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 【提问】基于Invest的水源涵养
    • ¥20 微信网友居然可以通过vx号找到我绑的手机号
    • ¥15 spring后端vue前端
    • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
    • ¥15 解riccati方程组
    • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
    • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
    • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
    • ¥50 树莓派安卓APK系统签名
    • ¥65 汇编语言除法溢出问题