1

I'm running magento on nginx.
My root is /pub
But there's some extensions trying to link to www.myurl.com/pub/xxx.file, so I want to rewrite all /pub as /

My plan was for www.myurl.com/pub/xxx.file to become www.myurl.com/xxx.file, but it is not working.

Here is my conf file (The abcdefg is a test directory):

server {
    listen 443 ssl http2;
    server_name www.myurl.com ;
    index index.html index.htm index.php default.html default.htm default.php;
    root  /home/wwwroot/www.myurl.com/pub;

    ssl on;
    ssl_certificate /etc/letsencrypt/live/www.myurl.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/www.myurl.com/privkey.pem;
    ssl_session_timeout 5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_ciphers "myciphers";
    ssl_session_cache builtin:1000 shared:SSL:10m;
    ssl_dhparam /usr/local/nginx/conf/ssl/dhparam.pem;

    location /pub {
           rewrite /pub /;
    }

    location ^~/abcdefg {
           rewrite /abcdefg/ /1234567/;
    }

    include other.conf;
    if (!-e $request_filename) {
            rewrite ^(.+)$ /index.php;
    }
    set $MAGE_MODE production;

    include enable-php.conf;

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
        expires      30d;
    }

    location ~ .*\.(js|css)?$
    {
        expires      12h;
    }

    #location ~ /.well-known {
    #    allow all;
    #}

    #location ~ /\.
    #{
    #    deny all;
    #}

    access_log  /home/wwwlogs/shopkey.doyustudio.com.log;
    error_log  /home/wwwlogs/shopkey.doyustudio.com.error.log debug;
}
Bill
  • 136
  • 12
楊翔宇
  • 13
  • 3

1 Answers1

1

instead of setting root /home/wwwroot/www.myurl.com/pub; use root /home/wwwroot/www.myurl.com/; and then create a location with try on pub dir, like this

location / {
    try_files /pub/$uri $uri;
}

This is saying to search the $uri first on the pub directory, if it doesnt exist then try on the root directory

Diego Velez
  • 825
  • 1
  • 7
  • 13
  • I tried this but not work. Then I read some docs about "try_files". And I found "try_files /pub/$uri $uri 404;" works. Thank you! – 楊翔宇 Nov 28 '17 at 11:07