-4

The webpage is accessed in the following way:

`example.com/` or `example.com/?site=website` 

In this example, I'm accessing the websites located on example.com domain.

Could anyone suggest an optimal way of rewriting rules, so the above would be shown and accessed by example.com/website, or just example.com/ if no site is sent?

nginx, Centos 7

UPDATE

Tried the following:

location / {
    index index.php;
    rewrite ^/(website)$ /site?$1;
}
  • Serverfault is not a configuration writing service. – Tero Kilkanen Aug 01 '17 at 07:27
  • But where should I write this? – Tarlan Mammadzada Aug 01 '17 at 07:35
  • I would vote to close, but I can't because of the bounty. "Questions should demonstrate reasonable business information technology management practices". – Tim Aug 05 '17 at 18:16
  • It seems like you recent updates to the question actually make it LESS clear — you originally was saying that the `site` string was a parameter (`?site=`), now you're saying it's an URI (`/site`). So, which one is it? The correct way to write any software is to first have a spec. If you have absolutely no spec that you could articulate, and only an account what doesn't work (but no account of what was supposed to happen, and what does work), then you won't get anywhere, even if you do ask other folks for help. – cnst Aug 05 '17 at 19:29

2 Answers2

1

Almost an identical question just a couple of weeks ago:

https://stackoverflow.com/questions/45257380/nginx-rewrite-query-string-args-to-html-url-w-o-backend-support/45293264#45293264

https://gist.github.com/cnst/3521404dfdf5cb7b4c526b5c6dff38ff

location = / {
    if ($arg_site) {
        return  302 /$arg_site;
    }
    return  200
    "<!DOCTYPE html><title>site</title>
    <form><input name='site' value='website'/></form>\n";
}
location / {
    rewrite     ^/([^/]*)\$     /?site=$1   break;
    proxy_pass  http://localhost:7381;
}

The idea is you create a redirect loop, but not really, since there is a difference between external v. internal redirects, so, the loop is not a closed one, because one redirect is explicitly only external, whereas the other one is explicitly only internal.

cnst
  • 13,848
  • 9
  • 54
  • 76
  • It gave socket() failed (24: Too many open files) while connecting to upstream – Tarlan Mammadzada Aug 05 '17 at 14:38
  • It sounds like you may be doing a proxy_pass to the very same server; you must do a proxy_pass to your actual backend, 100% distinct from the configuration at stake; if it's still nginx that's doing backend work for you, then you must use appropriate `fastcgi_pass` et al in place of `proxy_pass`. Please note that this configuration works 100% if your problem is any similar to what the other poster was having. Your question has very little info to know what exactly is it that you want, and what is actually working. If nothing at all works, and you have no idea what's going on — hire someone. – cnst Aug 05 '17 at 19:21
0

This should work. Basically it captures the string after the slash, before the end of the string, and calls site with that string as the parameter

location /
  rewrite ^/(website)$ /site?$1;
}

The browser needs to use a URL such as http://example.com/abcd , internally Nginx will call http://example.com/site=abcd . You obviously need to have a location set up that will respond to requests on that url. For example

location /site
  proxy_pass localhost:1234;
}

I did a test using a plain Nginx configuration, which only differs from the above with the .html extension required for the test.

server {
  listen 80;
  server_name test1.example.com;

  location / {
    root /var/www/test1;
    rewrite ^/(website).html$ /site.html?$1;
  }    
}

I created two files, website.html and site.html, with contents being their filename. Without the rewrite the browser shows "website". With the rewrite it shows "site". This proves to me the test configuration works.

Tim
  • 31,888
  • 7
  • 52
  • 78
  • Updated question, adding more details – Tarlan Mammadzada Aug 05 '17 at 00:10
  • The rewrite I gave you earlier works as per your edit when the URL doesn't contain the substring "website". Did you try it? – Tim Aug 05 '17 at 03:28
  • No, that didn't work for me – Tarlan Mammadzada Aug 05 '17 at 09:08
  • Updated the description – Tarlan Mammadzada Aug 05 '17 at 09:10
  • That's because your configuration is really quite incorrect. You shouldn't have the php location nested inside the root location. You should really be hiring someone to do this, not asking for help on Server Fault, we can't teach you Nginx configuration. – Tim Aug 05 '17 at 09:14
  • Ok, I removed it from there. Nothing changed. Updated description – Tarlan Mammadzada Aug 05 '17 at 09:20
  • Two problems here 1) You don't have a location to handle the site URL 2) Saying "doesn't work" is completely useless - you need to edit your question (not reply to a comment) to show a curl, the access logs, describe what happened, and why it isn't what you want. I believe I've answered your question, you can't keep changing it to try to get someone to build a full configuration for you when you don't understand Nginx at all. – Tim Aug 05 '17 at 18:15