Unable to view nginx a cached static webpage on nginx - nginx

I wish to view a cached static webpage on nginx through this URL:
http://localhost:8087/mycache/welcome_page.html
The welcome_page.html is kept in this location on Windows:
C:\nginx-1.22.1\html\welcome_page.html
The tricky part is that I have a reverse proxy setup using upstream with backend tomcat servers.
Despite specifying the location block for mycache the request goes to backend tomcat and thus fails with error the page you are looking for is currently unavailable. instead of looking for the cache HTML file on nginx.
An error occurred.
Sorry, the page you are looking for is currently unavailable.
Please try again later.
If you are the system administrator of this resource then you should check the error log for details.
Faithfully yours, nginx.
Below is my nginx configuration:
Can you please suggest how can i fix the problem?
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream tomcatcluster {
server 127.0.0.1:8181;
server 127.0.0.1:8282;
}
server {
listen 8087;
server_name localhost;
location /mycache/ {
root C:\nginx-1.22.1\html;
index index.html index.htm;
}
location / {
proxy_pass http://tomcatcluster;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
Can you please suggest how can we get this to work?

If you are manually adding the pages to C:\nginx-1.22.1\html, remove the proxy_pass line from the location /mycache/
location /mycache/ {
root C:\nginx-1.22.1\html;
index index.html index.htm;
}
Otherwise, let me know if what your are trying to achieve is to set the proxy_temp_path

Related

nginx not finding css file

I have just put my flask app on ubuntu. I am using gunicorn and nginx. The app is loading and nginx can find the templates. However the CSS files are not being served. If I enter the URL to access the CSS file in the browser I get a 403.
Here is my config file
server {
listen 80;
server_name myip;
location /static {
alias /home/ubuntu/enrich/website/static;
}
location / {
proxy_pass http://localhost:8000;
include /etc/nginx/proxy_params;
proxy_redirect off;
}
}
Any idea where I am going wrong?
Thx
S

How to add subdomain names and redirect them to local servers in nginx

After reading a few instructions and finding only half answers, here goes a small request:
I have a dynamic ip with a ddns service pointing "example.com" to my ip. Locally, my router redirects the access port to a win2016 server on which I run Nginx and I am trying to configure it so that I can access different local servers (server1:port1, server1:port2, server2:port3) using "subdomain.example.com".
So I am missing something, as all I managed to do is redirect the main example.com to anything I want OR redirect "example.com/name" to a local server.
What should I do to be able to access "server1:port1, server1:port2, server2:port3" using "subdomain1.example.com, subdomain2.example.com, subdomain3.example.com"
I am pulling my hair out.
I removed everything I tested in nginx.conf and all that remains is attached - which redirects to one of my servers.
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name "example.com";
location / {
proxy_pass http://127.0.0.1:"port";
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}

How to replace Nginx default error 400 "The plain HTTP request was sent to HTTPS port" page with Play! Framework backend.

I have a website using Play! framework with multiple domains proxying to the backend, example.com and example.ca.
I have all http requests on port 80 being rewritten to https on port 443. This is all working as expected.
But when I type into the address bar http://example.com:443, I'm served nginx's default error page, which says
400 Bad Request
The plain HTTP request was sent to HTTPS port
nginx
I'd like to serve my own error page for this, but I just can't seem to get it working. Here's a snipped of my configuration.
upstream my-backend {
server 127.0.0.1:9000;
}
server {
listen 443;
ssl on;
ssl_certificate /etc/ssl/certs/example.crt;
ssl_certificate_key /etc/ssl/private/example.key;
keepalive_timeout 70;
server_name example.com;
add_header Strict-Transport-Security max-age=15768000; #six months
location / {
proxy_pass http://my-backend;
}
error_page 400 502 error.html;
location = /error.html {
root /usr/share/nginx/html;
}
}
It works when my Play! application is shut down, but when it's running it always serves up the default nginx page.
I've tried adding the error page configuration to another server block like this
server {
listen 443;
ssl off;
server_name example.com;
error_page [..]
}
But that fails with the browser complaining about the certificate being wrong.
I'd really ultimately like to be able to catch and handle any errors which aren't handled by my Play! application with a custom page, or pages. I'd also like this solution to work if the user manually enters the site's IP into the address bar instead of the server name.
Any help is appreciated.
I found the answer to this here https://stackoverflow.com/a/12610382/4023897.
In my particular case, where I want to serve a static error page under these circumstances, my configuration is as follows
server {
listen 443;
ssl on;
ssl_certificate /etc/ssl/certs/example.crt;
ssl_certificate_key /etc/ssl/private/example.key;
keepalive_timeout 70;
server_name example.com;
add_header Strict-Transport-Security max-age=15768000; #six months
location = /error.html {
root /usr/share/nginx/html;
autoindex off;
}
location / {
proxy_pass http://my-backend;
}
# If they come here using HTTP, bounce them to the correct scheme
error_page 497 https://$host:$server_port/error.html;
}

Setting up nginx to support custom domain name

I have a Django web application hosted on a VM with the Debian-based Ubuntu as the OS, and nginx reverse proxy + gunicorn as the webserver.
The DNS of this web application is myapp.cloudapp.net. I also have a ccTLD mydomain.pk I need to be configured as a custom domain name for this web application.
My original registrar only supported nameservers. Thus I made an account on dns.he.net (a free DNS hosting provider) to host my nameservers, and set up the CName for my machine.
My problem is that once I set up the CName to point to my web app's DNS, entering mydomain.pk in the browser merely shows me a generic Welcome to ngnix! page. Whereas, entering myapp.cloudapp.net (or myapp.cloudapp.net:80) in the browser correctly opens up the web application. Why isn't setting up the CName working?
I've talked to the support staff at dns.he.net - I've been told my CName is set up correctly, and that there might be some problem with my nginx configuration.
For instance, here's my etc/nginx/sites-available/myproject file:
server {
listen 80;
server_name myapp.cloudapp.net;
charset utf-8;
underscores_in_headers on;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/myuser/projectpk/project;
}
location /static/admin {
root /home/myuser/.virtualenvs/projectpk/local/lib/python2.7/site-packages/django/contrib/admin/static/;
}
location / {
proxy_pass_request_headers on;
proxy_buffering on;
include proxy_params;
proxy_pass http://unix:/home/myuser/projectpk/project/project.sock;
}
error_page 500 502 503 504 /500.html;
location = /500.html {
root /home/myuser/projectpk/project/templates/;
}
}
Remove the server_name line, it's not needed in nginx unless you want to serve different content depending on the host name you receive.
If you remove that line, nginx will answer any request that arrives to your server at the proper port (80 in this case), coming with myapp.cloudapp.net or mydomain.pk in the Host header.
This assumes that there is no other configuration in /etc/nginx/sites-enabled that would catch the requests.

Nginx Cache Control breaks working paths

I'm following the html5boilerplate nginx setup for the most part, but everything keeps breaking when I include expires.conf.
HTML5BP for Nginx setup: https://github.com/h5bp/server-configs/tree/master/nginx
I've changed only a few very minor things which I'll put below. When in include conf/expires.conf however everything returns 404.
As a sidenote, I don't think it's just HTML5BP either.. i also followed this guide and images also broke (under the heading for Nginx tip #5 static assets expire).
location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ {
access_log off;
log_not_found off;
expires 360d;
}
I'm using nginx on a server behind an EC2 elastic load balancer.
Here is my /etc/nginx/sites-enabled/example.conf:
server {
listen 80;
server_name localhost;
location / {
root /var/www/html/example.com;
index index.html index.htm;
}
# Specify a charset
charset utf-8;
# Custom 404 page
error_page 404 /404.html;
include conf/base.conf;
}

Resources