I can't change path server name on nginx - nginx

I can't change path server name on nginx for example.com to example.com/home1 at (root /var/www/html/public ). please you recommend how to config ? Thank you.
server {
listen 80;
listen 443 ssl;
server_name example.com;
return 301 http://example.com/home1;
root /var/www/html/public;
index index.php index.html index.htm;
location / {
try_files $uri $uri /index.php?$args;
}

/home1 should be in the location selector instead. Remove the equals before /home1 if you want it to match anything with /home1 instead.
server {
listen 80;
listen 443 ssl;
server_name example.com;
root /var/www/html/public;
index index.php index.html index.htm;
location = /home1 {
try_files $uri $uri /index.php?$args;
}
}

Related

If I try to delete wave slash from URL, /index answers 404 Not Found

I'm working on a docker Nginx server on local, this is my conf.d/default.conf configuration:
server {
listen 80;
listen [::]:80;
server_name localhost;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name localhost;
ssl_certificate /etc/ssl/certs/localhost.pem;
ssl_certificate_key /etc/ssl/private/localhost.key;
root /usr/share/nginx/html;
index index.html index.htm;
location / {
try_files $uri $uri/ /index.html;
}
location ~ ^/~(.+?)(/.*)?$ {
alias /home/$1/public_html$2;
index index.html index.htm;
autoindex on;
}
}
This configuration works perfectly, but for last "location" block, which works as an "userDir" directive, I have to type an URL like this: https://localhost/~user1/
In order to remove that trailing slash, I tried:
location ~ ^/(.+?)(/.*)?$ {
alias /home/$1/public_html$2;
index index.html index.htm;
autoindex on;
}
This solution works fine and answers with the index.html content in "/home/user1/public_html" by using the URL https:/localhost/user1 which is how I want it to work, but then, root content (https:/localhost/) becomes 404 Not Found.
Hope I did explain it well and someone has a solution! Thank you in advance!!

How to configure multiple react projects using nginx?

I have 3 different React projects, pointing to the same IP address with different ports.
Routing works accurately for the first project(default project)
For the other 2, routing works fine if I'm navigating from the very first page of the website.
For an instance, if I'm at some.ip:3000 then I click something and now, I'm at some.ip:3000/page, it works fine
but if I try some.ip:3000/page directly, 404 page is returned.
Following is the nginx configuration - /etc/nginx/sites-available/default
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name <private-IP>;
root /var/www/<project1>;
index index.html index.htm;
location / {
try_files $uri $uri/ /index.html =404;
}
}
server {
listen 3000;
listen [::]:3000;
server_name <private-IP>;
root /var/www/<project2>;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
}
server {
listen 8000;
listen [::]:8000;
server_name <private-IP>;
root /var/www/<project3>;
index index.html index.htm;
location / {
try_files $uri $uri/ /index.html =404;
}
}

Nginx redirection from any subfolder to root index.html

Anything put as a subfolder, it will read the domain.com/index.html
Similar to this but in Nginx
Redirect URL non existent subfolder to root
server {
listen 80;
listen [::]:80;
server_name example.com;
root /apt/website/path;
index index.html;
location / {
try_files $uri $uri/ /index.html;
}
}

Nginx configuration causing too many redirects

I have a conf file with example.com as the root. In the example.com directory, there is an html, css, img, and js folder. I understand this deviates from the traditional html directory as root. I have tried many different configurations (using regex based on filetypes, variables, etc.) but I always get too many redirect errors. Can anyone help on a good conf file for this type of directory structure? Here is my conf file currently.
server {
listen 80 default_server;
listen [::]:80 default_server;
# return 301 https://$server_name$request_uri;
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
include snippets/ssl-example.com.conf;
include snippets/ssl-params.conf;
server_name www.example.com example.com;
root /var/www/www.example.com/;
index index.php index.html;
client_max_body_size 100m;
error_page 404 = error.html?error=404;
location ~ /.well-known {
allow all;
}
location / {
location ~* \.(html|php)$ {
root html/;
}
location ~* \.css$ {
root css/;
}
location ~* \.js$ {
root js/;
}
location ~* \.(png|jpeg|gif)$ {
root img/;
}
try_files $uri =404;
}
}
Thanks in advance for any help!
Here is the configuration I ended up using:
server {
listen 80 default_server;
listen [::]:80 default_server;
# return 301 https://$server_name$request_uri;
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
include snippets/ssl-example.com.conf;
include snippets/ssl-params.conf;
server_name www.example.com example.com;
root /var/www/www.example.com/;
index index.php index.html;
client_max_body_size 100m;
error_page 404 = /html/error.html?error=404;
location ~ /.well-known {
allow all;
}
location = / {
try_files /html/index.html =404;
}
location / {
location ~* \.(html|php)$ {
try_files $uri /html/$uri =404;
}
location ~* \.css$ {
try_files $uri /css/$uri =404;
}
location ~* \.js$ {
try_files $uri /js/$uri =404;
}
location ~* \.(png|jpeg|gif)$ {
try_files $uri /img/$uri =404;
}
try_files $uri =404;
}
}
My problem was that all of my redirects were using relative pathing (such as try_files html/$uri) instead of absolute pathing from the site root (/html/$uri). This lead to redirects like /html/html/html/...
I thought that if I used an absolute path, it would be absolute to the root of the server, and not the site.
My only issue now is that my parameter on my error page redirect (?error=404) doesn't work with absolute pathing, but that not a huge deal.

nginx how to configure route to html file?

I have the nginx config:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/www/site/public;
index main.html;
server_name localhost;
location / {
try_files $uri $uri/ =404;
}
At root directory I also have html files:page1.html, page2.html, page3.html.
I would like to configure route mysite.com/services/page1 to file page1.html. etc. How can I do it?
I tried it:
location = /services/page1 { try_files /page1.html;}
But it doesn't work.
If you want to rewrite url only if the file doesn't exist you can use named location in try_files directive.
location /services {
try_files $uri $uri/ #service_pages;
}
location #service_pages {
rewrite ^/services/page([1-3]).html /page$1.html;
}

Resources