简体   繁体   中英

Nginx rewrite rule for a subfolder

Im trying to rewrite a url like below:

https://example.com/products/product-post ---> https://example.com/product-post

But i want in the same time the url https://example.com/products/ to be accessible, without any modifications since its a product catalog.

This is not working causing a 500 server error:

location /products/ {
    rewrite ^/(.*)$ /products/$1 last;
}

For organizing reasons i keep my files in /products/file1, file2 etc. Perhaps i should be using "alias" and not "rewrite"?

Thank you.

UPDATE: Below is my server.conf config

server {
    server_name     www.example.com;
    listen          80;
    listen          443 ssl spdy;
    listen          [::]:80;
    listen          [::]:443 ssl spdy;
    ssl_certificate /opt/ssl/example.com.chained.crt;
    ssl_certificate_key /opt/ssl/example.com.key;
    # Non-www redirect
    return          301 https://example.com$request_uri;
}
server {
    server_name     example.com;
    listen          443 ssl spdy;
    listen          [::]:443 ssl spdy;
    root            /home/html_public;
    charset      UTF-8;
    ssl_certificate /opt/ssl/example.com.chained.crt;
    ssl_certificate_key /opt/ssl/example.com.key;
    add_header Strict-Transport-Security 'max-age=31536000; includeSubDomains; preload';
    ssl_prefer_server_ciphers on;
    ssl_ciphers "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH";
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_session_cache   shared:SSL:10m;
    ssl_session_timeout 10m;
    keepalive_timeout   70;
    ssl_buffer_size 1400;
    ssl_stapling on;
    ssl_stapling_verify on;
    resolver 8.8.8.8 8.8.4.4 valid=86400;
    resolver_timeout 10;
    ssl_trusted_certificate /opt/ssl/example.com.chained.crt;
location ~* \.(jpg|jpeg|gif|png|ico|cur|gz|svgz|mp4|ogg|ogv|webm|htc|css|js|otf|eot|svg|ttf|woff|woff2)(\?ver=[0-9.]+)?$ {
    expires 1M;
    add_header Pragma public;
    add_header Cache-Control "public, must-revalidate, proxy-revalidate";
    access_log off;
    }
    #access_log  logs/host.access.log  main;
    #===BAN COUNTRIES START ==============
    if ($allowed_country = no) {
    return 403;
    }
    if ($bad_referer) {
    return 444;
    }
location @extensionless-php {
    rewrite ^(.*)/$ $1.php last;
    rewrite ^(.*[^/])$ $1/ permanent;
    }
location / {
    try_files $uri $uri/ @extensionless-php;
    #limit_conn num_conn 15;
    #limit_req zone=num_reqs;
    }
    #=====PAGE SPEED START==========
location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" {
    add_header "" "";
    }
location ~ "^/pagespeed_static/" { }
location ~ "^/ngx_pagespeed_beacon$" { }
    #=====PAGE SPEED END==========
error_page  404    /404.php;
#pass the PHP scripts to FastCGI server listening on php-fpm unix socket
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 = /robots.txt {
    access_log off;
    log_not_found off;
    }
location ~ /\. {
    deny  all;
    access_log off;
    log_not_found off;
    }
#=====START phpMyAdmin==============#
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;
    }
#=====END phpMyAdmin==============#
location /webmail {
    root /usr/share/;
    index index.php index.html index.htm;
    location ~ ^/webmail/(.+\.php)$ {
    root /usr/share/;
    try_files $uri =404;
    fastcgi_pass unix:/tmp/php5-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include       fastcgi_params;
    }
    location ~* ^/webmail/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
    root /usr/share/;
    }
}
    #====Out Link REDIRECTS===============
    location /go/ {
    rewrite ^/go/(.*)$      /go/site-linker.php?site=$1 last;
    }

location ~ /products/(.+) {
    rewrite ^/products/(.*)$ /$1 last;
  }
}

In the logs im getting this:

2016/04/24 12:07:45 [notice] 6810#0: *1 "^/products/(.*)$" does not match "/xperia-z5/", client: 192.168.10.2, server: 192.168.10.1, request: "GET /xperia-z5/ HTTP/1.1", host: "192.168.10.1"
2016/04/24 12:07:45 [notice] 6810#0: *1 "^/products/(.*)$" does not match "/xperia-z5/", client: 192.168.10.2, server: 192.168.10.1, request: "GET /xperia-z5/ HTTP/1.1", host: "192.168.10.1"
2016/04/24 12:07:45 [notice] 6810#0: *1 "^(.*)/$" matches "/xperia-z5/", client: 192.168.10.2, server: 192.168.10.1, request: "GET /xperia-z5/ HTTP/1.1", host: "192.168.10.1"
2016/04/24 12:07:45 [notice] 6810#0: *1 "^(.*)/$" matches "/xperia-z5/", client: 192.168.10.2, server: 192.168.10.1, request: "GET /xperia-z5/ HTTP/1.1", host: "192.168.10.1"
2016/04/24 12:07:45 [notice] 6810#0: *1 rewritten data: "/xperia-z5.php", args: "", client: 192.168.10.2, server: 192.168.10.1, request: "GET /xperia-z5/ HTTP/1.1", host: "192.168.10.1"
2016/04/24 12:07:45 [notice] 6810#0: *1 rewritten data: "/xperia-z5.php", args: "", client: 192.168.10.2, server: 192.168.10.1, request: "GET /xperia-z5/ HTTP/1.1", host: "192.168.10.1"

https://www.digitalocean.com/community/tutorials/understanding-nginx-server-and-location-block-selection-algorithms I've updated my rewrite sample config - https://github.com/TeXXaS/nginx-configs/tree/master/rewrite From that tutorial - if you use rewrite , index or something like that nginx will internally reprocess request. To deal with that - I've added prods directory - so for .../products/ nginx serves /prods/index.html - while for all other .../products/(.+) - request is redirected to root. So if you type http://localhost:10080/products/index.html - it will show index.html from root, with out changing path in your browser. Hope it helps... :)

After a lot of trial and error, the sulution turned out to be:

location / {
    try_files $uri $uri/ @extensionless-php;
    rewrite /(.+$) /products/$1 break;
    }
location = /products/ {
    index index.php;
}

I hope this will help someone in the future.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM