I've been having a lot of trouble with rewrite directives - nothing seemed to actually work, so I tried making the most basic rewrite sample that I could:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /var/www/html;
location / {
rewrite ^/a$ /test.html break;
}
}
I was assuming this would return the contents of /var/www/html/test.html at http://localhost/a but it doesn't, it gives me a 404. Permissions on test.html are set correctly and it is accessible at http://localhost/test.html. I'm assuming rewriting is actually disabled - but I can't find anything like that in nginx.conf. The settings file above is the default in sites-available, and other than adding rewrite_log on; to the nginx.conf file everything is stock as distributed by the nginx 1.12.1 package in Ubuntu (17.10).
Logs provide no useful information and enabling rewrite logging has added no extra information to the logs. And yes, I've restarted nginx after each edit of the settings.
I've been trying all sorts of combinations, so at this point I'm just assuming rewrites aren't working/enabled? I'm totally confused here - ANY hints would be a big help.
I have no idea why but changing break to last made it work. This is confusing to me, as with this configuration I wouldn't expect there to be any processing after the rewrite. This is a pretty fresh install and there's not actually anything else on it yet that I would think would be interfering - so there must be some reason for this? If anyone has a better answer I'll switch the accepted, but for now this 'worked'.
Related
i don't understand what i'm doing wrong so i hope somebody can help :)
When i access http://10.0.0.54/index.html i get the right page but if i try to access http://10.0.0.54 instead of showing the index file it redirects me to https://10.0.0.54 showing error 502 bad gateway.
This is the configuration /etc/nginx/sites-available/default
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html/salvaderi;
index index.html;
server_name _;
location ~ /.well-known/acme-challenge {
allow all;
root /var/www/html/salvaderi;
}
location / {
root /var/www/html/salvaderi;
index index.html;
}
}
I am running nginx 1.18.0 on ubuntu 22.04
i tried changing parameters inside location /{} but i always get the same result. I also changed the root directory and made sure permission where set right. Searching on for the solution i saw other people having problems about PHP and FastCGI but i am not using it.
Your configuration about to be right.
Possible there is some kind of proxy or load-balancer is placed between you and nginx you configuring since you got redirect to HTTPS whether there is no any redirection instructions in your config and, in the same time, there is no listen 443 ssl in config, but you still got response on HTTPS request.
I'd check next:
Is 10.0.0.54 in fact IP of your server?
Is there any return 301, return 302 or rewrite instructions in your nginx config (the better
way is to dump config with nginx -T command and look over).
Didn't
you previously have configured some redirects that may have been
cached by your web client previously? Try to send GET request with
curl instead of web browser (if browser been used for tests).
I have a VPS that was serving static files using Apache. After covering some mileage in Django, I decided to change from Apache to Nginx. I thought it would be a simple matter (e.g. specifying the root folder for the domain, that settles it). I see that my expectations were unrealistic because now I am getting "404 Not Found" on all paths except the root folder.
mysite.conf:
server {
listen [::]:443 ssl;
listen 443 ssl;
server_name share.mysite.com;
root /var/www/html/share.mysite.com;
location / {
index index.html index.php;
}
}
How do I scale this configuration to serve static files located in different folders in the hierarchy?
1. share.mysite.com/tutorials/a-subject/a.mp4
2. share.mysite.com/tutorials/another-subject/something.jpg
3. share.mysite.com/some-folder/somefile.zip
At the moment, any of the above combinations give me a "404 Not Found", all except "https://share.mysite.com". Yet, the files are there.
I understand that this may involve the "location" keyword but I haven't seen a lucid explanation that translates to my case. I seek understanding. Nginx is new to me.
It is because of the rewritten rules. You had them in place when using Apache as most probably WordPress automatically created a .htaccess file and placed the necessary default rewrite rules for the so-called "pretty" URL ( e.g. accessing /blog /contact etc. ). In order to do that in Nginx, you have to add a few lines in the vhost for your domain there, please refer to the official documentation for that:
https://www.nginx.com/resources/wiki/start/topics/recipes/wordpress/
I'm a noob in nginx. I start configuring my domains (marianamarques.ntr.br and fabricadevozes.com.br) dns to point to my aws ec2 instance public ip. Thats ok.
When i start configure the nginx:
i created /var/www
i created /var/www/marianamarques.ntr.br/public_html
i created /var/www/fabricadevozes.com.br/public_html
i created /etc/nginx/sites-availble/marianamarques.ntr.br.conf listening to port 80 with root pointing to /var/www/marianamarques.ntr.br/public_html
i created /etc/nginx/sites-availble/fabricadevozes.com.br.conf listening to port 80 with root pointing to /var/www/fabricadevozes.com.br/public_html
When i tell on browser http://www.fabricadevozes.com.br i got htmls from /var/www/fabricadevozes.com.br/public_html but when i tell on browser http://www.marianamarques.ntr.br i got htmls from /var/www/fabricadevozes.com.br/public_html too.
I'm a bit desperated. My nginx was installed from apt-get and after hours and hours of web searches i know my /etc/nginx/conf.d/nginx.conf was missing (i don't have this file) but my nginx server starts with no issues.
Anybody can help?
nginx.conf should be located in /etc/nginx.
Post it along with your config files in sites-enabled and it'll be easier to tell you exactly what's wrong, but sounds like you may have a mistake in the server_name or root directives in your server definition. Make sure you specify the server name with and without the www. It could be loading your default domain if you didn't specify www. in the server name
server {
listen 80;
server_name marianamarques.ntr.br www.marianamarques.ntr.br;
root /var/www/marianamarques.ntr.br/public_html;
...
}
If thats not it, we'd need to see the config files.
Solved based on the link Nginx no-www to www and www to no-www - i create a second server listening to same port but expecting the server_name with-www prefix and then rewrite this to my other server point to without-www prefix like this:
server {
server_name www.domain.com;
rewrite ^(.*) http://domain.com$1 permanent;
}
server {
server_name domain.com;
#The rest of your configuration goes here#
}
I expect it can help others. Thanks a lot for everything! And sorry... my english is very poor!
Apparently you solved the problem but let me explain why it happened, you should read How nginx processes a request
Quote:
In this configuration nginx tests only the request’s header field
“Host” to determine which server the request should be routed to. If
its value does not match any server name, or the request does not
contain this header field at all, then nginx will route the request to
the default server for this port. In the configuration above, the
default server is the first one — which is nginx’s standard default
behaviour. It can also be set explicitly which server should be
default, with the default_server parameter in the listen directive
When you had a www or a non-www site missing, nginx couldn't match it to any server so it sends the request to the default server, and assuming you removed the default file from sites-enabled and didn't set any as default then the default server is the first one alphabetically, if we compare marianamarques.ntr.br.conf vs fabricadevozes.com.br.conf then the winner is the one starting with an f, that's why it was showing that server instead.
And since you did the basic redirection server, let me add that you better have used return over rewrite, check taxing rewrites
server {
server_name www.example.com;
return 301 http://example.com$request_uri$is_args$query_string;
}
I've seen some limited resources on checking for cookies with Nginx, but I couldn't really find the answer I was looking for, hopefully some of you Nginx masters can give me a hand.
Essentially I have a vhost that I'd like to redirect to a different domain unless the user has a cookie, here is what I've created:
server {
listen 80;
server_name example.com;
if ($http_cookie ~* "dev_cookie" ) {
root /home/deploy/apps/example/current/public;
passenger_enabled on;
rack_env production;
break;
}
rewrite ^/(.*) http://beta.example.com/$1 permanent;
}
But it doesn't seem to work, I get the error:
[emerg]: "root" directive is not allowed here in /opt/nginx/conf/nginx.conf:45
I'm not sure how to proceed here, any ideas guys?
That makes sense.
I would define another virtual host (beta.example.com) with that different root folder
and upon encountering cookie - do a rewrite
You can't set different roots for a domain conditionally, but you can redirect (rewrite) to another domain conditionally
This guy's example helped me a bit ago
http://nicknotfound.com/2009/01/12/iphone-website-with-nginx/
I've got a domain example.com and an "alternative" of some-example.com. I'm trying to direct traffic from some-example.com to example.com using a simple server declaration in nginx as follows:
server {
listen 80;
server_name some-example.com;
rewrite ^/(.*) http://example.com/$1 permanent;
}
I'm not 100% sure if this is the right rule, but I've got another vhost on the server, this isn't where the problem is, but it's necessary to understand the issue I'm having.
server {
listen 8745;
server_name localhost;
<other stuff goes here>
}
Hitting <my server IP>:8745 will go to that vhost, that works as intended. However I've got another vhost like this:
server {
listen 8746;
server_name localhost;
<other stuff goes here>
}
But all of my requests to <my server IP>:8746 hit example.com. I'm... baffled, I don't really grok nginx, so any help would be appreciated as to why this is happening. I mentioned that rule in the beginning because I'm thinking it has something to do with this. If additional information is needed I can supply it.
(Also, would this be better for Server Fault?)
I asked this on Server Fault also, however I found out the cause on my own. The below excerpt is pulled from this question.
It turns out this isn't an nginx
issue. I probably should've noted that
<my server IP>:8746 runs a Wordpress
installation; Wordpress has an option
set (in the wp_options table, the row
has option_id of 2 for me and
option_name of siteurl) to <some
domain>.com, I changed that to <some
domain>.com:8746 and it worked fine.