nginx.conf rewrite not getting triggered - nginx

http{
server {
listen 80;
server_name example.com;
rewrite_log on;
root /etc/nginx/html/mysite;
location ^~ /me/ {
rewrite ^/me/(.*)$ /etc/nginx/html/mysite/$1.html;
}
}
}
The above is not working when I make the request http://example.com/me/all
I want it to be served by /etc/nginx/html/mysite/all.html
But the request goes to /etc/nginx/html/mysite/me/all ignoring the rewrite and nothing about a rewrite error in the logs (just a 404 not found).

You are specifying the root of your virtual host to /etc/nginx/html/mysite (line 6). In your rewrite rule you again specify the whole path /etc/nginx/html/mysite/$1.html (line 8). What nginx does is appending the target of your rewrite rule (/etc/nginx/html/mysite/$1.html) to the root of your virtual host (/etc/nginx/html/mysite). In you example this results in searching for the file:
/etc/nginx/html/mysite/etc/nginx/html/mysite/all.html
Such file does not exist and causes the 404.
Correct the rule to make the target a relative path with respect to root, as:
http{
server {
listen 80;
server_name example.com;
rewrite_log on;
root /etc/nginx/html/mysite;
location ^~ /me/ {
rewrite ^/me/(.*)$ /$1.html;
}
}
}
Remember that you can get more information on these errors by reading the nginx error log (by default placed at /var/log/nginx/error.log). In you case it would have stated something like:
"/usr/share/nginx/html/mysite/usr/share/nginx/html/mysite/all.html" failed (2: No such file or directory), client: 127.0.0.1, server: localhost, request: "GET /me/all HTTP/1.1", host: "localhost"

Related

nginx execute incorrect path with alias

I don't understand my nginx not serve file from '/var/www/html/foo/' when I execute 'w3m http://localhost/foo/test.html'?
I get 404 error, despite of existing test.html in '/var/www/html/foo/'. When I checked log I see that it looking for page in '/var/www/html/nginx/foo/test.html'.
user nobody nogroup;
worker_processes 2;
events {
worker_connections 512;
}
http {
server {
listen *:80;
listen *:1026;
server_name "test";
root /var/www/html/nginx/ ;
location foo/ {
alias /var/www/html/foo/ ;
}
}
}
arek#127:~$ ls /var/www/html/nginx
test.html
arek#127:~$ ls /var/www/html/foo
index.html test_bigger.html test.html text.html
arek#127:~$
When I checked log I see that it looking for page in '/var/www/html/nginx/foo/test.html'
2022/09/03 02:36:05 [error] 139475#139475: *2 open() "/var/www/html/nginx/foo/test.html" failed (2: No such file or directory), client: 127.0.0.1, server: test, request: "GET /foo/test.html HTTP/1.0", host: "localhost"
when I change my root path on '/var/www/html/' its works.
Try it with the server configuration block like this instead, which is working on my server. Removed the trailing slash from the root, and added a slash before foo/. Also added a default_type for the location. If you still get an error, comment with the log showing the query path.
server {
listen 80;
listen 1026;
server_name "test";
root /var/www/html/nginx;
location /foo/ {
alias /var/www/html/foo/;
default_type "text/html";
}
}

nginx appears to use rewrite regardless of server_name

I have the following config file.
server {
listen 80;
server_name web.example.com;
access_log /var/www/example/shared/log/web.access.log;
error_log /var/www/example/shared/log/web.error.log debug;
rewrite ^/(.*)$ http://www.example.net$request_uri; permanent;
}
When I make a request for curl -Ii -H "Host: example.com" http://example.com the above rewrite rule works. (ARGH)..
The server_name explicitly says "web.example.com"
2014/11/18 22:49:20 [notice] 30694#0: 1868 "^/(.)$" matches "/", client: 1.2.3.4, server: web.example.com, request: "HEAD / HTTP/1.1", host: "example.com"
2014/11/18 22:49:20 [notice] 30694#0: *1868 rewritten redirect: "http://www.example.net/", client: 1.2.3.4, server: web.example.com, request: "HEAD / HTTP/1.1", host: "example.com"
Not present here is the other server { } configs. Xavier (below) pointed out that I had set default_server for listen: 443; but not for listen: 80. (argh)
That's not the strict solution to the issue.
What's happening is that you have only one server block and it becomes the default server block for all requests, even the ones not matching the server name. You simply need to add a default server block in your configuration :
server {
listen 80 default_server;
}
By the way, you have a typo (semicolon before permanent) and you don't need a rewrite as you have specific regular expression. Use this instead :
return 301 http://www.example.net$request_uri;
After a while...
I found that I needed the location / { } wrapped around it
server {
listen 80;
server_name web.example.com;
access_log /var/www/example/shared/log/web.access.log;
error_log /var/www/example/shared/log/web.error.log debug;
location / {
rewrite ^/(.*)$ http://www.example.net$request_uri permanent;
}
}

Alias uses --prefix instead of server block's root

I'm trying to set up a nginx server and alias my static files.
server {
# Listen on localhost:8000;
listen 8000;
# Should be the root
root /Users/rouvenherzog/Documents/projects/nd;
# host matches localhost
server_name localhost;
location / {
proxy_pass http://localhost:5000;
}
location /favicon.ico {
root /Users/rouvenherzog/Documents/projects/nd/n/n/static/img;
}
location ~ /([\w]+)/n/static/(.*) {
alias n/n/static/$2;
}
location ~ /([\w]+)/nb/static/(.*) {
alias nb/nb/static/$2;
}
}
When request static files, it looks for them in the nginx --prefix folder ( which is /usr/local/Cellar/nginx/1.6.0_1 ), instead of the root folder.
For instance:
open() "/usr/local/Cellar/nginx/1.6.0_1/n/n/static/neb/js/javascript.js" failed
(2: No such file or directory),
client: 127.0.0.1,
server: localhost,
request: "GET /pages/n/static/neb/js/javascript.js HTTP/1.1",
host: "localhost:8000",
referrer: "http://localhost:8000/pages/n/"
Any idea why ?
Thank you very much!
As #akawhy suggested, using a rewrite instead of alias works and respects the root path.
server {
# Listen on localhost:8000;
listen 8000;
# Should be the root
root /Users/rouvenherzog/Documents/projects/nd;
# host matches localhost
server_name localhost;
location / {
proxy_pass http://localhost:5000;
}
location /favicon.ico {
root /Users/rouvenherzog/Documents/projects/nd/n/n/static/img;
}
location ~ /([\w]+)/n/static/(.*) {
rewrite ^([\w]+)/n/static/(.*)$ /n/n/static/$2 break;
}
location ~ /([\w]+)/nb/static/(.*) {
rewrite ^([\w]+)/nb/static/(.*)$ /nb/nb/static/$2 break;
}
}
Because your alias directive used a relative path. I think you should use a absolute path instead.
You may check this nginx alias+location directive

Location block is ignored

I testing out nginx but I bumped into a severe problem. Somehow the location tag doesn't catch my uri's. When the browser, or I manually try to access /favicon.ico on my localhost, it just throws an 404 error. The configuration looks like the following code:
server {
listen 80;
server_name localhost;
root www;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
index index.html index.php;
}
location = /favicon.ico {
alias alias /assets/images/favicon.ico;
}
... and the error log looks like this:
2014/01/17 00:05:18 [error] 7408#10036: *82 CreateFile() "C:\Users\user\Webb\nginx/www/favicon.ico" failed (2: FormatMessage() error:(15105)), client: 127.0.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "localhost"
The log proves then that the location block was ignored, because the path nginx look for the file is not the one alias points to.
AFAIK nginx alias goes like this:
alias <the base path>
means that with it you tell the nginx the part to the left of your first "/" in the URI.
example. suppose I have my images in /var/www/html/images and my application refers to apic.jpg as http://app.com/pictures/apic.jpg , what I do then is this:
location ^/pictures {
alias /var/www/html/images;
}
hope this helps.

nginx proxy_pass configuration

I need to proxy a couple of urls to different hosts. Actually, I'm using the same host with different port to test my nginx configuration. This is my virtual host definition:
server {
listen 8081;
server_name domain.com;
location /Plasmid/ {
proxy_pass http://localhost:8000/Plasmid/;
}
location /_community/ {
proxy_pass http://localhost:8082/comments_api/ ;
}
location / {
# rewrite cq_user_authenticated===(.*)/(.*)/iuuid=(.*)/commenti.html$ /Plasmid/comments/legacy/$3/$1 ;
# rewrite querystring===(.*)$ /Plasmid/comments/legacy/$1 ;
# rewrite cq_user_authenticated===([^&]*)&/.*uuid=([^/]*) /comments_api/legacy/$2 ;
# rewrite userdetails(.*) /Plasmid/comments/user_details ;
root html;
index index.html index.htm;
}
}
Of course my hosts file has mapping for the domain.com
When I call the url: http://domain.com:8081/Plasmid/default/page/12 I get an http 404
If I remove the second location from my configuration:
location /_community/ {
proxy_pass http://localhost:8082/comments_api/ ;
}
I get the resource I want, but some part are missed since the are hosted on a different platform:
[error] 1033#0: *1 open() "/usr/local/Cellar/nginx/1.2.6/html/_community/content
How can I resolve this issue?
Do a little change:
location ^~ /Plasmid/ {
proxy_pass http://localhost:8000/Plasmid/;
}
location ^~ /_comunity/ {
proxy_pass http://localhost:8082/comments_api/;
Why is that? Because ^~ means starts with and when you request for page:
http://domain.com:8081/Plasmid/default/page/12
it fit to that rule. In your configuration you are using no mark and something like this:
location /anylocation
and it looks like your nginx prefer rule
location / {
than
location /Plasmid
and
location /_comunity
because it's using root directive and searching for _community/content in html folder (as you get in error message).
In other words ^~ has greater priority than no mark. One thing that could also help is to add break directive after each proxy_pass directive;

Resources