Forward nginx 8080 port to 80 - nginx

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;
}
}

Related

nginx reverse proxy - map server port to specific url

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)?

How to configure subdomains in nginx

I am trying to load another website into my subdomain using reverse proxy in Nginx
for example
www.example.com is my website
test.example.com is my subdomain
and I want to load www.facebook.com into the subdomain
Now if the user enters test.example.com Facebook will be loaded into this subdomain and I also have access to use its content, like I will be able to getelementbytag etc.
Please tell me how to do that, with proper steps because I am new in Nginx. Thanks
Here I just took this from one of my projects. It demonstrated that the server_name you specify in each server block will be matched against the host header in the incoming request.
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
location / {
return 301 https://$host$request_uri;
}
}
server {
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
# my domain
server_name mydomain.dev;
root /usr/share/nginx/html;
include /etc/nginx/snippets/ssl.conf;
location / {
root /usr/share/nginx/html;
index index.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
server {
listen 443 ssl http2 ;
listen [::]:443 ssl http2;
# my sub domain
server_name foobar.mydomain.dev;
location / {
proxy_pass https://foobarbaz.website-eu-central-1.linodeobjects.com/;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}

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;
}
}

Host multipe sites on nginx

Currently my nginx.conf looks like this:
server {
listen 80; ## listen for ipv4; this line is default and implied
listen [::]:80 default ipv6only=on; ## listen for ipv6
root /usr/share/nginx/html;
index index.html index.htm;
# 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;
}
# Attempt to load static files, if not found route to #rootfiles
location ~ (.+)\.(html|json|txt|js|css|jpg|jpeg|gif|png|svg|ico|eot|otf|woff|woff2|ttf)$ {
try_files $uri #rootfiles;
}
# deny access to . files, for security
#
location ~ /\. {
access_log off;
log_not_found off;
deny all;
}
location #rootfiles {
rewrite ^/(?:triangles|equation-of-a-line)/(.*) /$1 redirect;
}
sendfile off;
}
I basically have one site at root /usr/share/nginx/html;.
How can I change this to allow multiple sites with each site hosted at the same root directory?
Also, I have cname * mapped to the IP address.
How can I change this so that each website has its own cname?

How do you configure NGINX so that it handles 50x errors from upstream server B that upstream server A cannot?

I have recently started learning NGINX and encountered some problems. I am still getting and error 500, even if I have configured NGINX (1.4.6) with an error_page directive. The config works ( I get the woops.html as expected ) if I try not to send error 500 from the second upstream server. Here's my configuration file:
server {
listen 80 default_server;
root /srv/www;
index index.html index.htm;
server_name test.dev;
location / {
proxy_pass http://localhost:8080;
proxy_intercept_errors on;
error_page 500 #retry;
}
location #retry {
proxy_pass http://localhost:8081;
proxy_intercept_errors on;
error_page 500 /woops.html;
}
location = /woops.html {
root /srv/www;
}
}
server {
listen 8080;
listen localhost:8080;
server_name localhost;
root /srv/www;
location / {
return 500;
}
}
server {
listen 8081;
listen localhost:8081;
server_name localhost;
root /srv/www/app;
location / {
return 500;
}
}

Resources