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;
}
}
Related
I have been trying to get this to work for most of the afternoon and have tried several of the solution I have seen online with no luck. I have tried putting the $uri twice, but cant get the try_files to pick up my other images size. Any help would be greatly appreciated (exmaples).
server {
listen 80;
server_name mysite.com;
root /web/mysite.com;
location / {
index index.html index.htm;
}
# directory based redirects
location /mimg/ {
alias /web/mysite.com/images/;
# cache expiration
expires 30d;
# set default error page
set $error_page_404 /samples/_default.jpg;
# failover chain samples (16:9 content flag set)
location ~* ^/samples-304x171/(.*)$ {
set $error_page_404 /samples/_default-16x9.jpg;
}
location ~* ^/samples-400x225/(.*)$ {
try_files $uri /samples-304x171/$1;
}
location ~* ^/samples-640x360/(.*)$ {
try_files $uri /samples-400x225/$1;
}
location ~* ^/samples-800x450/(.*)$ {
try_files $uri /samples-640x360/$1;
}
# set default image
error_page 404 $error_page_404;
} # end of /mimg
}
I have the following server in NGINX and it works fine. But, I am wondering is it possible to add text to a response from a remote URL where hosts my before_body.txt and after_body.txt? Is there any way to tackle this?
server {
listen 80;
root /storage/path;
index index.html;
server_name test.domain.com;
location / {
try_files $uri $uri/ =404;
add_before_body /src/before_body.txt;
add_after_body /src/after_body.txt;
autoindex on;
}
location /src/ {
alias /storage/path/content/;
}
}
I have resolved with replacing the alias as follows:
location /src/ {
proxy_pass https://externalserver.com/;
}
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.
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/;
}
}