server {
listen 8000;
server_name local.server;
# root /Users/username/code/project/register;
# Works if root defined here and location / is deleted
location /register { # root seems to be set to location / always
root /Users/username/code/project/register;
try_files $uri $uri/ /index.html?/$request_uri;
}
location / {
root /Users/username/code/web/public;
try_files $uri $uri/ /index.html?/$request_uri;
}
}
local.server:8000/register always try to server file from the root of location /
I want
location / -> root code/web/public
location/register -> root code/project/register
Is that possible ?
I suspect that your value for root is incorrect. You are pointing to /Users/username/code/project/register/register.
Try:
location /register {
root /Users/username/code/project;
...
}
Also, the default action under /register is to go to /index.html which is under the other root. Did you mean to specify /register/index.html?
For example:
location /register {
root /Users/username/code/project;
try_files $uri $uri/ /register/index.html?/$request_uri;
}
Related
Is it possible to have a suburl that point to a different root? For example:
www.domain.com/ -> /home/ubuntu/project1
www.domain.com/project2 -> /home/ubuntu/project2
I have this configuration at this moment but I'm getting a 404 when resolving domain.com/project2
server {
listen 80;
server_name domain.com;
root /home/ubuntu/project1;
location /project2 {
root /home/ubuntu/project2;
index index.html;
}
location / {
try_files $uri $uri/ /index.html;
}
}
It's because nginx will append the uri to root directive.
In your example config, accessing domain.com/project2 would try to look for a file named project2 in /home/ubuntu/project2 which is not found and return 404.
To solve your problem, try using alias directives.
server {
listen 80;
server_name domain.com;
root /home/ubuntu/project1;
location /project2 {
alias /home/ubuntu/project2;
index index.html;
}
location / {
try_files $uri $uri/ /index.html;
}
}
I am newbie to nginx server. I am getting stuck in URL redirection. I have following lines to default file.
server {
listen 80;
root /home/ubuntu/web/server/current/public;
index index.php index.html index.htm index.nginx-debian.html;
server_name _;
error_log /home/ubuntu/web/error.log info;
location / {
# try_files $uri $uri/ /index.php?$query_string;
# try_files $uri $uri/ =404;
}
rewrite ^/web/(.*) /web/;
location /web/ {
alias /home/ubuntu/web/client/web/;
# try_files $uri $uri/ index.html;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
what I expect from above rewrite rule is - all URLs like - http://example.com/web/login, http://exmpale.com/web/dashboard will be redirected to /home/ubuntu/web/client/web/ and the default page to hit is index.html file.
When I open the error log file then i found error like -
rewrite or internal redirection cycle while internally redirecting to "/web/index.php", client: ip_address, server: _, request: "GET /web/ HTTP/1.1", host: "ipaddress"
What i am doing wrong here.
Answer credit goes to #RichardSmith, he provided possible error which is in right direction. I figure out my mistake in nginx rewriting rule.
rewrite ^/web/(.*) /web/;
location /web/ {
alias /home/ubuntu/web/client/web/;
# try_files $uri $uri/ index.html;
}
Instead of above I should have following-
rewrite ^/web/(.*) web/;
location web/ {
alias /home/ubuntu/web/client/web/;
# try_files $uri $uri/ index.html;
}
Server consider path as absolute whenever / placed before web. Thus rewrite statement tries to redirect to fallback file which is not existed in absolute path. Eventually, rewrite statement makes never ending loop.
The current nginx conf I have looks like:
server {
listen 80;
server_name mydomain.com;
root /home/myname/some_app/public;
location / {
try_files $uri #some_named_location;
}
location /sub {
root /home/myname/other_app/public;
try_files $uri #other_named_location;
}
}
I expect mydomain.com/sub/xxx to be served by /home/myname/other_app/public/sub/xxx, but instead it's served by /home/myname/some_app/public/sub/xxx. What's going wrong here?
I also tried using alias instead of root in the /sub location block:
location /sub {
alias /home/myname/other_app/public;
try_files $uri #other_named_location;
}
Then I expect mydomain.com/sub/xxx to be served by /home/myname/other_app/public/xxx, but still it's served by /home/myname/some_app/public/sub/xxx.
I even tried moving the server block's root directive into the / location block.
server {
location / {
root /home/myname/some_app/public;
try_files $uri #some_named_location;
}
location /sub {
root /home/myname/other_app/public;
try_files $uri #other_named_location;
}
}
But it still doesn't work.
I have a directory/files structure such as:
root/
a/
utils.js
b/
assets/
styles.css
app.js
index.html
And I want to configure nginx to serve files from a directory directly if exist and have single page app in directory b (if file in path exists the it wil be served directly, nd if not the fallback will end up at index.htm file.
For example:
myapp.com/a/utils.js will return that file.
myapp.com/b/ or myapp.com/b/foo will display index.html
myapp.com/b/assets/style.css will return directly css file
I tries multiple different configurations and non had worke so far. For exampe the simplest:
server {
listen 80;
root /root;
index index.html;
location / {
try_files $uri $uri/ /index.html =404;
}
}
I also tries something to serve different directories:
server {
listen 80;
root /root;
index index.html;
location /a {
try_files $uri $uri/ =404;
}
location /b {
try_files $uri $uri/ /index.html =404;
}
}
I tried to define different roots as well:
server {
listen 80;
index index.html;
location /a {
root /root/a;
try_files $uri $uri/ =404;
}
location /b {
root /root/b;
try_files $uri $uri/ /index.html =404;
}
}
Nginx seems to ignore existing files and ends up returning 404 page at all times. When I try to access soe existing file directly it gets redirected to / (root) url regardless.
The last parameter of a try_files statement is the default action. There can only be one. Many of your examples have two. See this document for details.
The correct URI for your index.html file is /b/index.html which is what you need to use for the default action of the try_files statement.
This should meet your requirements:
location / {
try_files $uri $uri/ /b/index.html;
}
You do not state what should happen with the URI /a/foo. In the above case, it would also return index.html. If you need it to return a 404 response, you would use:
location / {
try_files $uri $uri/ =404;
}
location /b {
try_files $uri $uri/ /b/index.html;
}
See this document for more.
I have really simple nginx configuration with 3 locations inside. Each of them have it's own root directory + I should be able to add another in the future easily.
What I want:
Request /admin => location ^/admin(/|$)
Request /admin/ => location ^/admin(/|$)
Request /admin/blabla => location ^/admin(/|$)
Request /client => location ^/client(/|$)
Request /client/ => location ^/client(/|$)
Request /client/blabla => location ^/client(/|$)
Request /blabla => location /
Request /admin-blabla => location /
Request /client-blabla => location /
Actual result:
All requests goes to location /.
I tried many different suggestions from docs, stackoverflow and other sources using different combinations of aliases, try_files, roots and regexes, but nothing worked for me.
Only when I tried to use just return 200 'admin'; and return 200 'front' it worked as intended.
Minimal config:
server {
listen 80;
index index.html;
location / {
root /var/www/html/www_new/front;
try_files $uri $uri/ /index.html;
}
location ~ ^/admin(/|$) {
root /var/www/html/www_new/admin;
try_files $uri $uri/ /index.html;
}
location ~ ^/client(/|$) {
root /var/www/html/www_new/client;
try_files $uri $uri/ /index.html;
}
}
Directory structure:
/admin
/client
/front
Thank you
When you change the root it'll still include the directory name, so what you want to do is only set the root for location /. You also don't need any additional regex on /admin as the location modifier ~ already tells nginx 'anything starting with'.
This works for your use case:
server {
listen 80;
index index.html;
location / {
root /var/www/html/www_new/front;
try_files $uri $uri/ /index.html;
}
location ~ ^/admin {
root /var/www/html/www_new; # the directory (/admin) will be appended to this, so don't include it in the root otherwise it'll look for /var/www/html/www_new/admin/admin
try_files $uri $uri/ /admin/index.html; # try_files will need to be relative to root
}
}