NGINX Rewrite Rule without access to the configuration file - nginx

In apache, the rewrite rule can be written in the configuration file or in .htaccess file. How about in nginx? Can I use url rewriting without access to the configuration file?

Unfortunately, you can't. This is one of the reasons shared hostings typically use apache or litespeed, not nginx or lighttpd.
A (very ugly) workaround would be to handle all requests with a script which would contain the rewrite rules and would serve the file/script according to the request URI (and which could be modified by a user without having root privileges). However you'd have a bad performance serving static files and you'd need to handle all the request headers by this script, which is not very practical.

Related

Web root option in Varnish Cache similar to `root` in nginx

In nginx we have root option to serve files from a specific directory, eg: root /var/www/data/ in nginx conf, if my url is https://mydom.com/$file_name, nginx will look for files present in /var/www/data/$file_name and return the file if present otherwise return 404.
Now, I want something similar option in Varnish. Is there a way where I can serve files from a specific directory? How can I tell varnish to look for files in a specific directory and return that file?
Varnish is a cache, not a webserver. Varnish doesn't serve pages from a document root on the disk, but it caches responses that came from a pre-defined backend server.
Although Varnish and Nginx have some similarities, and cover some of the same use cases, they are entirely different products.
However, if you use Nginx as a reverse proxy, instead of a webserver, it won't use the root option either.
There is one way you can make Varnish act like a webserver, and that is by leveraging the file module in Varnish Enterprise. This allows Varnish to serve files from disk, but this is not available in the open source version of Varnish, only in the commercial version.

Is it possible and how to use NGINX for upload files?

I use NGINX for serving of static files which (it offloads the backend which serves only "dynamic" requests). But now I need to upload files to server. Is it possible to use NGINX for uploading too? Also it will be good if there is a way to set permissions of uploading files... How to use NGINX for uploading? Which protocol? Module? Some example configuration?
I found only WebDAV but its module looks outdated, also I never used WebDAV before so I am not sure how good is it (I mean performance). What is a typical solution? Do I need to write something, some plugin/module?
There is no built-in module to do the job. There is a third-party upload module.

Is there a way loading an existing page under a fake url?

I want to rewrite an existing path page to a fake directory.
www.website.com/company/events -> fake path, it doesn't exist but it should load /events/ page.
www.website.com/events
Is there a way to do it in Wordpress?
This is what I tried in .htaccess but it does't work.
RewriteCond %{REQUEST_URI} ^/events/ [NC]
RewriteRule .* /company/events/ [L]
P.S: I want to mention the /events/ page is generated by a WP plugin called Events Calendar.
Thanks!
Sure that is possible:
RewriteEngine on
RewriteRule ^/?company/events/?$ /events
An alternative would be an alias:
Alias /company/events /events
You obviously need to integrate this with the wordpress generated rewriting rules, something we cannot really help with. Especially since you did not post those...
This rule will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

What is the default file to be visited behind a website?

When I open a website's url such as www.stackoverflow.com via curl, which file is actually being visited in the server? I know usually it is index.html. But I cannot find such a convention in the RFC2616 document. How can I know it?
BR
The document dilivered by calling a website without a path in the URL is configured by the webserver. So you have no standard there. Is a users joice.
Curl will download the file the webserver is delivering him, or follow the redirect (if -L option is given) when webserver responses a redirect.
There is no way for the client to know how the data for the HTTP response was generated. It might not even be related to a specific file.
The last time I wrote a significant bit of server side code, everything outside of /static/ was routed (via mod_rewrite) though a FastCGI program that got its data from a few different controller libraries, a dozen database schema libraries, a database and a dozen template files.
The WWW is built on links between URLs, not files. Don't worry about files if you are writing client code.
It's not necessarily index.html, and you can't actually know that it could be anything depending on the Server Configuration, for instance in Apache you can change the directory index to the one that suits you
DirectoryIndex home.php
in this case the default file accessed is home.php
in IIS you can take a look about default index and how to change it
but the defaults are
in Apache
index.php (usually: depending on the server configuration)
index.html (is the default that comes with a fresh install)
in IIS
Default.htm
Default.asp
Index.htm
Index.html
Iisstart.htm

Nginx user defined rewrite rules

I'm wondering why this is question got not asked before: I want let the user make her own rewrite rules but not editing his configuration file.
/etc/nginx/sites-enabled/example.com: this file (or the original as this file is a symlinkn to sites-available...) is only writeable for root.
As for apache2 we can give him a way to write own rewrite rules, other expires configuration and so on without the need to edit the configuration file in /etc. Let's say it is a hosting provider and the user can't even access the /etc directory...
The whole .htaccess concept is not implemented in nginx, which one of the reasons why nginx is faster,
because if we have a file inside /var/www/site/folder1/folder2/file.php for example, the server needs to look for .htaccess inside folder2 and folder1 and site, and each will override the one above it, an overhead for each request.
The only way you might implement a kind of similar thing is add an include inside the config file that reads from a certain location
server {
# bla bla
include /var/www/site1/config.conf;
location bla {
# bla bla
}
}
The config file would include server level directives,
Or you could just skip the server block and include files, and those files should include server blocks, but this isn't good because you give them access to the whole server, not just theirs. eg:
# /etc/nginx/sites-enabled/site-name
include /var/www/site1/site.conf
include /var/www/site2/site.conf
# etc
Basically, my advice, if you really need this functionality, install apache, and maybe just use nginx to server assets and proxy to apache.

Resources