How to configure webhook in nginx? - nginx

I am trying to configure the nginx.conf file to receive webhook requests from an external website. The request gets failed with status code 405 (not allowed) when I try to call the webhook using postman. The path of the webhook is /hooks
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
# include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
# server_name _;
# root /usr/share/nginx/html;
server_name _;
root /etc/nginx/code/build;
location / {
try_files $uri /index.html;
}
error_log /var/log/nginx/jackfruit.error_log debug;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
#include /etc/nginx/sites-enabled/*;
#location / {
#}
location /api {
proxy_pass http://localhost:8000;
}
location /hooks/ {
proxy_pass http://localhost:8000/hooks;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}

Try removing the trailing slash from the location block as such:
location /hooks {
proxy_pass http://localhost:8000/hooks;
}
The reason why I suspect this may be an issue:
If a location is defined by a prefix string that ends with the slash character, and requests are processed by one of proxy_pass, fastcgi_pass, uwsgi_pass, scgi_pass, or memcached_pass, then in response to a request with URI equal to this string, but without the trailing slash, a permanent redirect with the code 301 will be returned to the requested URI with the slash appended.

Related

Ngnix settings can not change the default index.html

I config a static site via nginx, and include my settings under conf.d directory. listen port 80.
But I found when I request the url, nginx always redirect the default page /usr/share/nginx/html/index.html
My configuration does seem work, for all access logs were written to my access log settings, and if I change index.html to some other name(i.html for example) in my directory, and request url mysite.com/i.html, I can access the correct page.
So, it seems that nginx redirect all index.html to the default one.
I tried change default port/server name/root and annotate the default settings, even close selinux, all above doesn't work, it really make me mad.
Can anyone help me?
I'm using nginx version 1.10.2, on CentOS 7.2
and following is my site settings:
# the upstream component nginx needs to connect to
upstream blog {
server 0.0.0.0:80; # for a web port socket (we'll use this first)
}
# configuration of the server
server {
# the port your site will be served on
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
# the domain name it will serve for
server_name mysite.com;
charset utf-8;
access_log /var/log/blog/access.log;
error_log /var/log/blog/error.log;
# max upload size
client_max_body_size 75M; # adjust to taste
# Finally, send all non-media requests to the Django server.
location / { try_files $uri #blog; }
location #blog{
root /home/work/projects/blog/public/;
index index.html index.htm;
uwsgi_read_timeout 120s;
uwsgi_send_timeout 120s;
}
}
and the /etc/nginx/nginx.conf is
server {
# listen 8000 default_server;
# listen [::]:8000 default_server;
# server_name not;
# root /usr/share/nginx/html;
# Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
# location / {
# }
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
This makes no sense:
location / {
try_files $uri #blog;
}
location #blog{
root /home/work/projects/blog/public/;
index index.html index.htm;
uwsgi_read_timeout 120s;
uwsgi_send_timeout 120s;
}
The lack of a root directive means that the first try_files will look for files in the default location.
All you need is root and index directives within the server context:
For example:
root /home/work/projects/blog/public;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
See this document for details.

How to Proxy Pass from / to /index.html

I'm currently working on a JS Project, that uses the url path. Now if I go on my website with example.com/, the JavaScript won't work, because I actually need example.com/index.html.
I'm already using an reverse proxy to proxy pass to two different docker containers. So my idea was to pass the request to example.com/index.html when example.com/ is called. But I can't figure out the regex stuff to achieve this goal.
My old config:
server {
listen 80;
server_name example.com;
# allow large uploads of files - refer to nginx documentation
client_max_body_size 1G;
# optimize downloading files larger than 1G - refer to nginx doc
before adjusting
#proxy_max_temp_file_size 2G;
location / {
proxy_pass http://structure.example:80;
}
location /cdn {
proxy_pass http://content.example:80;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
Stuff I tried:
server {
listen 80;
server_name example.com;
# allow large uploads of files - refer to nginx documentation
client_max_body_size 1G;
# optimize downloading files larger than 1G - refer to nginx doc
before adjusting
#proxy_max_temp_file_size 2G;
location / {
proxy_pass http://structure.nocms:80/index.html;
}
location ~* \S+ {
proxy_pass http://structure.nocms:80;
}
location /cdn {
proxy_pass http://content.nocms:80;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
The accepted answer has one disadvantage: going to example.com explicitly redirects to example.com/index.html (that is, returns 301 Moved permanently), which is not always desired.
Instead, I suggest to prepend location / with another directive, location = /, which is designed to the root URL only:
location = / {
proxy_pass http://structure.nocms:80/index.html;
}
location / {
proxy_pass http://structure.nocms:80;
}
The above instructs nginx to pass requests to example.com directly to http://structure.nocms:80/index.html, while requesting any other URLs in example.com/* would pass the request to the corresponding URL in the downstream.
Below config should work for you
server {
listen 80;
server_name example.com;
# allow large uploads of files - refer to nginx documentation
client_max_body_size 1G;
# optimize downloading files larger than 1G - refer to nginx doc
before adjusting
#proxy_max_temp_file_size 2G;
location = / {
rewrite ^ /index.html permanent;
}
location / {
proxy_pass http://structure.example:80;
}
location /cdn {
proxy_pass http://content.example:80;
}
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?

Use nginx to serve static files from subdirectories of a given directory

I have several sets of static .html files on my server, and I would like use nginx to serve them directly. For example, nginx should serve an URI of the following pattern:
www.mysite.com/public/doc/foo/bar.html
with the .html file that is located at /home/www-data/mysite/public/doc/foo/bar.html. You can think of foo as the set name, and bar as the file name here.
I wonder whether the following piece of nginx config would do the job:
server {
listen 8080;
server_name www.mysite.com mysite.com;
error_log /home/www-data/logs/nginx_www.error.log;
error_page 404 /404.html;
location /public/doc/ {
autoindex on;
alias /home/www-data/mysite/public/doc/;
}
location = /404.html {
alias /home/www-data/mysite/static/html/404.html;
}
}
In other words, all requests of the pattern /public/doc/.../....html are going to be handled by nginx, and if any given URI is not found, a default www.mysite.com/404.html is returned.
It should work, however http://nginx.org/en/docs/http/ngx_http_core_module.html#alias says:
When location matches the last part of the directive’s value:
it is better to use the root directive instead:
which would yield:
server {
listen 8080;
server_name www.mysite.com mysite.com;
error_log /home/www-data/logs/nginx_www.error.log;
error_page 404 /404.html;
location /public/doc/ {
autoindex on;
root /home/www-data/mysite;
}
location = /404.html {
root /home/www-data/mysite/static/html;
}
}

nginx: how to always return a custom 404 page for the default host

I have nginx 0.8.53 configured with some virtual hosts which work as desired. However, due to nginx's "best match" on virtual hosts, I need to add a default host to catch all requests that aren't for a specific virtual host. I would like the default host to return a custom 404 page that I created instead of the default nginx 404 page.
I assumed I needed something like:
# The default server:
server {
listen 80 default_server;
server_name everythingelse;
# Everything is a 404
location / {
return 404;
}
error_page 404 /opt/local/html/404.html;
}
But this still returns the default nginx 404 page. It seems the return 404 ignores the error_page config.
Here what I have in my conf to make it work:
# The default server.
server {
listen 80 default_server;
server_name everythingelse;
error_page 404 /404.html;
# Everything is a 404
location / {
return 404; #return the code 404
}
# link the code to the file
location = /404.html {
#EDIT this line to make it match the folder where there is your errors page
#Dont forget to create 404.html in this folder
root /var/www/nginx/errors/;
}
}
Very few directives in nginx take a filesystem path. You want something like:
# The default server.
server {
listen 80 default_server;
server_name everythingelse;
root /opt/local/html;
error_page 404 /404.html;
# Everything is a 404
location / {
return 404;
}
# EDIT: You may need this to prevent return 404; recursion
location = /404.html {
internal;
}
}
Move the error_page directive up the conf to before you call return 404.
This should work:
# The default server.
#
server {
listen 80 default_server;
server_name everythingelse;
error_page 404 /error_docs/404.html;
# Everything is a 404
location / {
return 404;
}
# Custom Error Page
location /error_docs {
alias /opt/local/html/;
log_not_found off;
access_log off;
}
}
This will use the same custom one for all sites (servers). You need to add the error docs location.
http {
error_page 404 /error_docs/404.html;
...
# The default server.
#
server {
listen 80 default_server;
server_name everythingelse;
# Everything is a 404
location / {
return 404;
}
# Custom Error Page
location /error_docs {
alias /opt/local/html/;
log_not_found off;
access_log off;
}
}
}
Before the server block, you are probably using the include instruction :
include /etc/nginx/conf.d/*.conf;
server {
listen 80;
...
}
I had the same issue, and fixed it by removing the include line:
server {
listen 80;
...
}
In NGINX v1.14 (released in 2019-12-26) you cannot use location = /404.html. Removing the = (equals sign) works:
server {
listen 80;
server_name everythingelse;
error_page 404 /404.html;
location / {
return 404;
}
location /404.html {
root /opt/local/html;
}
}
Since both root and error_page are valid directives at http block scope, one can leverage the inheritance behavior of nginx configuration.
To share custom error pages between all my vhosts (so that requests on unknown vhosts or inexistent resources in known vhosts get my custom error pages as a response according to error_page definition), I use the following recipe.
1. Add those three lines to /etc/nginx/nginx.conf
# …
root /var/www/whatever # Used by undefined hosts
error_page 403 404 =404 /404.html
error_page 502 503 504 =500 /500.html
# …
2. Create /etc/nginx/sites-available/catchall with the following snippet as a «catch all» default virtual server.
server {
listen 80 default_server;
listen [::]:80 default_server;
# SSL rules here if required
server_name _;
}
3. Create 404.html and 500.html files in each document root where a custom error has to be used (or link the ones in /var/www/whatever), otherwise defaults will be used instead.
That said, not all directives can inherit from a higher level scope, and even worse, some inheritance is unintuitive and doesn't behave as you might expect.
Understanding the NGINX Configuration file structure and configuration context
Interesting SO thread: Location nesting
(Using Debian9 and nginx/1.10.3)

Resources