penulisan semula url - penulisan semula nginx: jika $uri bukan fail atau direktori, lompat ke $uri/
高洛峰
高洛峰 2017-05-16 17:25:56
0
2
678

Tatarajah

nginx 1.6.2

Permintaan

Lawati

$uri 

Jika

1)如果 存在文件 /Vhost/$http_host/$uri ,访问的是该文件
2)否则 如果 $uri 不是以 “/” 结尾 301 跳转到 $uri/
3)否则 如果 $uri 以 “/” 结尾 且 存在 /Cache/$http_host/$uri/index.html ,访问的是该文件
4)否则 指向 /index.php (ThinkPHP框架)

Contoh

访问 http://www.a.com/robots.txt -> /Vhost/www.a.com/robots.txt
访问 http://a.com/robots.txt ->301 跳转 ->  http://www.a.com/robots.txt
访问 http://www.c.com/sitemap.html -> /Vhost/www.c.com/sitemap.html
访问 http://www.d.com/game/ -> /Cache/www.d.com/game/index.html
访问 http://www.d.com/news/1000011 -> 301 跳转到  -> http://www.d.com/news/1000011/
访问 http://www.d.com/news/1000011/ -> /index.php (当该/Cache/www.d.com/news/1000011/index.html 不存在的时候)
访问 http://www.d.com/news/1000011/ -> /Cache/www.d.com/news/100011/index.html (当该/Cache/www.d.com/news/1000011/index.html 存在的时候)

Tatarajah

server {
    listen       80;
    server_name  wap wap.loc m.wap.loc wap.loc m.xxx.com game1.wap.loc game2.wap.loc game3.wap.loc;

    location / {
        root   d:/wap_dev;
        index  index.html index.htm index.php;          
        try_files $uri /Vhost/$http_host/$uri /Cache/$http_host/$uri/index.html /index.php?s=$uri&$args;            

        location ~ \.php$ {
            try_files $uri =404;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;    
        }
    }


    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }

    location ~ /\.(ht|svn|git) {
        deny all;
    }
}

Soalan

Di atas 1, 3, dan 4 telah dilaksanakan, dan 2 masih belum dilaksanakan.


Terdapat kesilapan taip pada point 3) di atas, ia adalah seperti berikut. Ia telah diubahsuai, tetapi sg tidak berubah

3)否则 如果 $uri 以 “/” 结尾 且 存在 /Cache/$http_host/$uri/index.html ,访问的是该文件

Supplement

1) 如何才可以 不是 以/结尾且不是以.xml结尾且不是以.html .htm结尾的才跳转

Sekarang, jika lompatan berakhir dengan /, penghujung .xml dan penghujung .html juga akan lompat
Saya menulis perenggan sendiri, tetapi gagal:

rewrite ([^\/]|\.xml|\.html?)$ $uri/ permanent;
高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

membalas semua(2)
小葫芦

Anda perlu menggunakan lokasi bernama bermula dengan @ untuk menyelesaikan masalah ini.

# 如果找不到任何匹配的文件就会进入 @rewrite location 来决定如何 rewrite
try_files $uri /Vhost/$http_host/$uri /Cache/$http_host/$uri/index.html @rewrite;

location @rewrite {
    # 如果末尾不是 /,301 跳转
    rewrite [^\/]$ $uri/ permanent;

    # 默认交给 index.php 来处理
    rewrite . /index.php?s=$uri&$args last;
}
小葫芦

Kerana peraturan anda bercanggah.

2)否则 如果 $uri 不是以 “/” 结尾 301 跳转到 $uri/
3)否则 如果 $uri 不是以 “/” 结尾 且 存在 /Cache/$http_host/$uri/index.html ,访问的是该文件

2 dan 3 jelas tidak boleh berpuas hati pada masa yang sama.

hendaklah ditukar kepada

2) else if $uri 不是以 “/” 结尾 then 301 跳转到 $uri/
3) else if $uri 以 “/” 结尾 and 存在 /Cache/$http_host/$uri/index.html then 访问该文件

sebenarnya bersamaan dengan

else if $uri 不是以 “/” 结尾 then 301 跳转到 $uri/
else if 存在 /Cache/$http_host/$uri/index.html then 访问该文件
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!