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.
Related
I have searched many articles and posts but didn't find any particular fix for my requirement. Hence, posting this question.
I have 2 locations on my server /p1 and /p2. The default location should be /p1/index.html.
I want, when I will access http://localhost:8080/p1 or http://localhost:8080/p2 then I should be able to get the data from http://localhost:8080/p1/index.html, and also the URL should change in the browser.
Can we use any other directives instead of location directives to achieve this?
Below is the current Nginx config :
server {
listen 80;
root /usr/share/nginx/html;
location / {
try_files $uri /p1/index.html;
}
location /p1 {
try_files $uri /p1/index.html;
}
location /p2 {
try_files $uri /p1/index.html;
}
}
Need in this way :
http://localhost:8080/p1 --> http://localhost:8080/p1/index.html
http://localhost:8080/p2 --> http://localhost:8080/p1/index.html
Any suggestion or help will be grateful.
Got some clue from #BijayRegmi's comment. I have modified little and the below changes worked for me.
server {
listen 80;
root /usr/share/nginx/html;
location / {
try_files $uri /p1/index.html;
}
location /p1 {
try_files $uri /p1/index.html;
}
location /p2 {
return 301 $scheme://$http_host/p1/index.html;
}
}
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;
}
}
What I'm trying to achieve is if someone visit my home/index page, I need to server my index.html file. But, if it's another URL/path pass the request to my index.php file (I'm using Symfony).
I follow this example, but is not working. It's always serving my PHP file.
server
{
listen 80;
server_name mysite.com;
root /path/to/my/web;
index index.html index.php;
location = /index.html
{
try_files $uri /index.html?$args;
}
location /
{
try_files $uri /index.php?$args;
}
}
I will appreciate any help or guidance you can share with me.
This finally worked for me:
server
{
listen 80;
server_name mysite.com;
root /path/to/my/web;
index index.html index.php;
location = /
{
try_files $uri /index.html?$args;
}
location /
{
try_files $uri /index.php?$args;
}
}
I have this folder structure:
/document/root/
|-- main
`-- wishlist
I want to get my nginx to work like this: If I point my browser to example.com/wishlist it will display the index.html on the wishlist folder. If I point my browser to example.com, I want it to fallback to main/index.html (and, of course, related main/style.css and other files that are in the main directory).
I don't want to write a location rule for every folder I have under my root, so I want this to be as generic as possible. I have found this questtion and it has helped me to get most of the job done, but there's something not working: if I point the browser to wishlist/index.html it works perfectly. But if I remove the index.html and point it just to example.com/wishlist the browser will return a 404. My current Nginx config is below. Can someone point me in the right direction? Thanks.
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
root /document/root/main;
location ~ ^/([^/]+)(/.+)?$ {
if (!-d "$document_root/$1") {
return 404;
}
try_files /$1$2 /main$2 =404;
}
}
All you need to do for the index file is:
index index.html
location / {
try_files $uri.html $uri/index.html =404;
}
location /wishlist {
try_files $uri.html $uri/index.html =404;
}
Turns out I found out a way that worked for me: using a custom #location on nginx. My final piece of code turned out something like this:
location / {
root /document/root/main;
index index.html;
try_files $uri $uri/ index.html;
}
location ~ ^/(.+)$ {
root /document/root;
index index.html;
try_files $uri $uri/ index.html #main;
}
location #main {
try_files /main/$uri /main/$uri/;
}
Now example.com uses /document/root/main as it's root and example.com/wishlist uses /document/root/wishlist :) Hope this helps someone else.
Keep it simple:
server {
root /document/root/main/;
index index.html;
location /wishlist {
root /document/root/;
}
}
If I give the following
server {
listen 80;
server_name mywebsite.com;
root /a/b/;
index index.html index.htm index.php;
location / {
try_files $uri $uri/ =404;
}
location /virtualLoc1 {
root /a/b/c;
location ~ \.php$ {
#PHP file forward to PHP-FPM
}
}
location /virtualLoc2/ {
alias /a/b/c/d/e/;
location ~ \.php(/|$) {
#PHP file forward to PHP-FPM
}
}
}
the http://mywebsite.com/virtualLoc2/ is not working where as http://mywebsite.com/virtualLoc1/ is working.
I'm using alias because I don't have virtual2 directory after /a/b/c/d/e/
but for virtualLoc1 there is a physical directory named virtualLoc1 after /a/b/d/c/virtualLoc1/.
Can anyone suggest what am I doing wrong here?