nginx忽略/ en网址

时间:2018-08-02 08:46:55

标签: nginx url-rewriting

我正在建立一个多语言站点。网址的必要检查(国家/地区,语言等将使用php完成)。我的问题是nginx中的重写规则。

在url中,我有www.domain.tld / something(实际上是something.php) 我需要将其重定向到www.domain.tld / en / something,但仅用于浏览器(nginx必须仍然“看到” www.domain.tld / something)。

基本上,我已经准备好处理所有的php文件来处理/en/script.php,但是我需要将URL更改为/ en / fr等,具体取决于用户在nginx中选择的语言。

我的/etc/share/nginx/config.d/domain.conf如下:

server {

    listen 443 ssl http2;
    listen [::]:443 ssl http2;

    server_name domain.tld www.domain.tld;

    ssl_protocols TLSv1.2 TLSv1.1 TLSv1;
    ssl_session_timeout  5m;
    ssl_ciphers HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers  on;
    ssl_session_cache shared:SSL:10m;

    error_page 404 /404.php;

    root        /usr/share/nginx/html/domain.tld;
    index       index.php index.html index.htm;
    access_log  /var/log/nginx/domains/domain.tld.log combined;
    error_log   /var/log/nginx/domains/domain.tld.error.log error;

    client_header_buffer_size 64k;
    large_client_header_buffers 4 64k;
    location = /favicon.ico {
            log_not_found off;
            access_log off;
    }

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

   location / {
            add_header X-Frame-Options SAMEORIGIN;
            rewrite ^/(.*)/$ /$1 permanent;

            rewrite ^/[a-zA-Z][a-zA-Z]/index.php(.*)$ /index.php$1;
            add_header Access-Control-Allow-Headers Content-Type;
            add_header Access-Control-Max-Age 86400;

            location ~ [^/]\.php(/|$) {
                    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                    if (!-f $document_root$fastcgi_script_name) {
                            return 404;
                    }

                    fastcgi_connect_timeout 60;
                    fastcgi_send_timeout 1800;
                    fastcgi_read_timeout 1800;
                    fastcgi_buffer_size 128k;
                    fastcgi_buffers 4 256k;
                    fastcgi_busy_buffers_size 256k;
                    fastcgi_temp_file_write_size 256k;

                    try_files $uri = index.php;
                    fastcgi_intercept_errors off;
                    fastcgi_index  404.php;
                    include        fastcgi_params;
                    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                    fastcgi_pass   php-fpm;
            }


        try_files $uri $uri.html $uri/ @extensionless-php;
        index index.html index.htm index.php;

    }

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


    location ~* "/\.(htaccess|htpasswd)$" {
            deny all;
            return 404;
    }
}

我很可能没有充分解释我的问题,所以请告知我其他问题。

谢谢。

0 个答案:

没有答案