nginx reverse proxy - map server port to specific url - nginx

by setting a default.conf as follows:
server {
listen 80;
listen [::]:80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
location /some-path {
proxy_pass http://<SERVER_IP>:8050;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
I'm able to make sure that the url http:/URL/some-path displays what is present at http:/URL:8050/some-path .
Which is the syntax to obtain that http:/URL/some-path displays what is present at http:/URL:8050 (i.e. without need to specify some-path in the second URL)?

Related

Set nginx to only monitor subdirectory

I'm trying to change nginx config file so that nginx only monitors localhost/test and stops monitoring localhost/.
I tried replacing the default file in /etc/nginx/conf.d/default.conf but I'm getting 404. I basically just changed location / { to location /test {
server {
listen 80;
listen [::]:80;
server_name localhost;
location /test {
alias /usr/share/nginx/html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
I'm unsure why this is not working. I also tried to change listen [::]:80; to listen [::]:80/test; but there I got an error invalid host in "80/test" of the "listen" directive
Edit:
I tried copying the index.html to /usr/share/nginx/html/test/ and changed alias in location to root but then when I navigated to localhost/test I got 403 Forbidden

NGINX 502 response after applying config

I have a dockerized app running on nginx. With the default config every time I reload the app and I'm not on the / route, I get a 404 Not Found error. I also want to always redirect my http traffic to https, so I put this config together.
server {
listen 80;
server_name _;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri /index.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
return 301 https://$host$request_uri;
}
But when I apply the config I always get The server encountered a temporary error and could not complete your request. Please try again in 30 seconds

Nginx - location redirective

I am trying below config but its not working as expected.
I do have index.html page under /book directory (/usr/share/nginx/html/book) and I need to access it from let say when I access localhost:8080/book it should show index.html under book directory.
its working when I try localhost:8080/book/ but not working for localhost:8080/book (without trailing slash '/'). can you please let me know where I went wrong. Thanks!
default.conf:
server {
listen 80;
listen [::]:80;
server_name localhost;
location = /book {
root /usr/share/nginx/html;
index index.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}

Forward nginx 8080 port to 80

I have a server with multiple nginx instances
One of them is running at 8080
server {
listen 8080;
server_name myip v2.example.com www.v2.example.com;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
proxy_pass http://v2.example.com:8080/;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
But I want to enter the site using example.com , instead of example.com:8080
Problem is that there is another server running at port 80 , I tried to fix it with the proxy pass, but it doesn't seem to work
How can I fix that?
You can try with ssl port 443. Of course you may need to add a valid ssl certificate to avoid error messages from the browser.
If you do not want to use the above method, you can use the proxy pass on one of your other instances as follows:
location /some-url/ {
proxy_pass http://localhost:8080/;
}
After that, You can then access it through: http://localhost:80/some-url/
Redirect with return. You can put the following in one file:
server {
listen 80;
listen [::]:80;
server_name v2.example.com www.v2.example.com;
return 301 http://$host$request_uri:8080;
}
server {
listen 8080;
server_name myip v2.example.com www.v2.example.com;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}

Nginx change document root directory

I'm using the Windows setup file from Nginx For Windows
But It doesn't allow to change the install location, so it defaults to C:\nginx Is there a way to update the config file to change the root directory to D:\blabla?
Sample code from nginx.conf
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
Use the -p parameter when you start nginx:
nginx -p "D:\blabla"
root d:/path/whereever;
will work without issues, but do use a proper windows version like the Butterfly or Caterpillar release and not those other crippled versions.
even a root '//192.168.2.5/mount/webdrive';
will work!
I don't know howit was two years ago but with an current Version (1.13.9) you only need to set the root of your Server
server {
listen 80;
server_name localhost;
location / {
root D:\blabla;
index index.html index.htm;
}
#error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root D:\blabla;
}
}

Resources