0

I want to rewrite only / and nothing else. Meaning

/ => /fp/

However I still want all other files to be able to accessed directly.

/login.php => still loads
/join.php => still loads
/index.php => still loads
etc.

I have tried several different ways of doing this, but it seems when it works it forwards ALL files in / instead of ONLY / when called by itself

I have tried things like:

rewrite ^ /fp/ break;
rewrite ^/ /fp/ break;

I have tried so many ways I can't think straight.

How do I do this?

Currently my config looks like:

location / {
    proxy_pass      http://192.227.210.138:8080;
    location ~* ^.+\.(jpeg|jpg|png|gif|bmp|ico|svg|tif|tiff|css|js|ttf|otf|webp|woff|txt|csv|rtf|doc|docx|xls|xlsx|ppt|pptx|odf|odp|ods|odt|pdf|psd|ai|eot|eps|ps|zip|tar|tgz|gz|rar|bz2|7z|aac|m4a|mp3|mp4|ogg|wav|wma|3gp|avi|flv|m4v|mkv|mov|mpeg|mpg|wmv|exe|iso|dmg|swf)$ {
        root           /home/account_name/web/domain.tld/public_html;
        access_log     /var/log/apache2/domains/domain.tld.log combined;
        access_log     /var/log/apache2/domains/domain.tld.bytes bytes;
        expires        max;
        try_files      $uri @fallback;
    }
    rewrite ^ /fp/ break;
}
Bruce
  • 103
  • 1
  • 5
  • Are you using nested location blocks deliberately? Two access_logs? Why is the /fp/ rewrite inside the / location block? – Tim Apr 30 '17 at 04:51
  • Are you using nested location blocks deliberately? Yes - I am not very fluent with nginx and it seemed the correct way to set location as / and then a second nested location to set the users root directory. Why is the /fp/ rewrite inside the / location block.... where else could it go that wouldn't try to re-write every single directory, file, and subdirectory? Like I say this is all very confusing. I have been trying to get this one single re-write working for over 10 hours. Seems no one really understanding re-writing with nginx. – Bruce Apr 30 '17 at 05:06
  • I'm not an Nginx expert, I just know what works for me. You can see my Nginx configurations [here](https://www.photographerstechsupport.com/tutorials/hosting-wordpress-on-aws-tutorial-pt1-introduction-configuration-downloads/#wpmu-nginx-configuration-files), with no nested location blocks required even hosting more than one service on the same domain and with some fairly extensive tweaking. Sorry can't really help with the rewrite, but like I said, I'd do it in a non-nested location block. Did you try my suggestion? I don't really understand what you're trying to achieve. – Tim Apr 30 '17 at 05:10
  • I wouldn't image most people need nested blocks unless they are hosting 50 or 60 different domains under 50 or 60 different user accounts. Using a configuration like yours I would have to manually change the config each time a new user account was created. As to what I am trying to do... very simple. visitor goes to http://domain.tld visitor has content loaded from http://domain.tld/fp/ (using a re-write not a redirect). If instead the user goes to http://domain.tld/something.php,(html,jpg,anything at all) that page loads without doing a re-write. – Bruce Apr 30 '17 at 05:20
  • Possible duplicate of [Nginx location exact match matches beyond arguement](https://serverfault.com/questions/490760/nginx-location-exact-match-matches-beyond-arguement) – Esa Jokinen Apr 30 '17 at 08:21

2 Answers2

0

You need an use an exact match, =. I don't know how to use rewrite, so I'd do it like this

location = / {
  alias /fp/;
}

That probably isn't quite right, but it should get you closer.

You might be able to use the = symbol with rewrite. Not sure.

Tim
  • 31,888
  • 7
  • 52
  • 78
  • I added my config to make it more clear. I am using the location as rewriting (nor alias) will work without them as I understand. – Bruce Apr 30 '17 at 00:53
0

It took me an incredible amount of time to figure out how to do this. I couldn't find any solutions anywhere online.

I want to stress to anyone who ever attempts this or reads this, I completely read the docs on why to avoid if statements and when they are acceptable. In this solution I believe they were not only acceptable - but the only way to achieve the desired outcome.

The solution was to do an if test on the uri to see if it was '/' and if so perform the re-write.

location / {
    if ($uri = '/'){
        rewrite / /fp/ break;
    }
    proxy_pass      http://192.227.210.138:8080;
    location ~* ^.+\.(jpeg|jpg|png|gif|bmp|ico|svg|tif|tiff|css|js|ttf|otf|webp|woff|txt|csv|rtf|doc|docx|xls|xlsx|ppt|pptx|odf|odp|ods|odt|pdf|psd|ai|eot|eps|ps|zip|tar|tgz|gz|rar|bz2|7z|aac|m4a|mp3|mp4|ogg|wav|wma|3gp|avi|flv|m4v|mkv|mov|mpeg|mpg|wmv|exe|iso|dmg|swf)$ {
        root           /home/account_name/web/domain.tld/public_html;
        access_log     /var/log/apache2/domains/domain.tld.log combined;
        access_log     /var/log/apache2/domains/domain.tld.bytes bytes;
        expires        max;
        try_files      $uri @fallback;
    }
}

Typically one should use try_files instead of if however from all my testing I couldn't figure out how to correctly write a try_files statement that would return only on the '/' and not '/index.php' and other such files.

As one can see from my included url's below this works absolutely as I asked in my question

http://adsactlyhits.com/ - this will return one page

http://adsactlyhits.com/index.php - this will return a different page

http://adsactlyhits.com/join.php - this page also still works
Bruce
  • 103
  • 1
  • 5
  • 1
    Good that you solved it. I really think there's likely to be a better way, and maybe someone will come along after the weekend who knows it. I think regular expressions or even just the = operator could play a part, I just don't have time to experiment and work it out right now. I suggest not marking the question answered quite yet, give it a couple of days. – Tim Apr 30 '17 at 07:34
  • "I couldn't find any solutions anywhere online." How about Serverfault? :) [Nginx location exact match matches beyond arguement](https://serverfault.com/q/490760/274176) has this + other approaches. – Esa Jokinen Apr 30 '17 at 08:20
  • The `location = /` solution is the best one one can do here. However, without knowing your full configuration, I cannot write a configuration that will accomplish the goal you are having. – Tero Kilkanen May 02 '17 at 10:52