nginx return 200 at other server_name - nginx

I have this code:
server {
listen 80;
server_name example.com;
return 301 https://www.sajufortune.com$request_uri;
}
server {
listen 80;
server_name www.example.com;
if ($http_x_forwarded_proto != 'https') {
rewrite ^ https://$host$request_uri? permanent;
}
set $alb internal-saju-alb-before-w43.us-west-2.elb.amazonaws.com;
location / {
proxy_pass $alb;
}
}
server {
listen 80;
location /ping/ {
return 200 'pong';
}
}
I got this returning 200 code from here:
I want to 200 response pong to /ping/ request from any url except example.com, www.example.com.
How can I do this?

Try the below config
server {
listen 80;
server_name example.com;
return 301 https://www.sajufortune.com$request_uri;
}
server {
listen 80;
server_name www.example.com;
if ($http_x_forwarded_proto != 'https') {
rewrite ^ https://$host$request_uri? permanent;
}
set $alb internal-saju-alb-before-w43.us-west-2.elb.amazonaws.com;
location / {
proxy_pass $alb;
}
}
server {
listen 80;
server_name _;
location /ping/ {
return 200 'pong';
}
}

According to http://nginx.org/en/docs/http/ngx_http_core_module.html#listen you need to either put the ping/pong server-block on the top, or append default_server on the listen option (e.g. listen 80 default_server;)

Related

Nginx : How to translate example.org to www.example.org

When I open example.org, I'd like my browser to translate it to www.example.org, how should I change my configuration to do so ?
server {
listen 80;
server_name example.org www.example.org;
...
}
You can just redirect:
server {
listen 80;
server_name example.org;
return 301 http://www.example.org;
}
server {
listen 80;
server_name www.example.org;
...
}

allow one location for server block otherwise return 404

server {
listen 80;
server_name my.example.com;
location ~ /\.well-known/acme-challenge {
root /var/www/html;
}
return 404;
}
I want to allow one location to be accessible on port 80 (for certbot) otherwise return 404. My code above doesn't work. If I iremove the return 404; line it works.
You can create a root location which returns 404:
server {
listen 80;
server_name my.example.com;
location ~ /\.well-known/acme-challenge {
root /var/www/html;
}
location / {
return 404;
}
}

In Nginx, I want to redirect the sub-domain request with request-uri to sub-domain but if there is no request-uri in it should redirect to main domain

My problem statement :
my domain : example.com
sub-domain : main.example.com
when we will access:
1. http://main.example.com/xyz or https://main.example.com/xyz :
It must be redirect to https://main.example.com/xyz
http://main.example.com or https://main.example.com :
It must be redirect to https://www.example.com
I am using nginx. What will be configuration file for Nginx server?
My current setting is :
server{
listen 443;
ssl on;
ssl_certificate /var/www/html/demo.crt;
ssl_certificate_key /var/www/html/demo.key;
server_name main.example.com$request_uri;
location / {
root /var/www/html;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
}
server {
listen 80;
server_name main.example.com$request_uri;
return 301 https://main.example.com$request_uri;
}
server {
listen 80;
server_name main.example.com;
return 301 https://www.example.com;
}
server {
listen 443;
server_name main.example.com;
return 301 https://www.example.com;
}
Try
server{
listen 80;
listen 443 ssl;
server_name main.example.com;
ssl_certificate /var/www/html/demo.crt;
ssl_certificate_key /var/www/html/demo.key;
location / {
proxy_pass https://www.example.com;
}
location ~ ^(/.+) {
return 301 https://main.example.com$1;
}
}
http://nginx.org/en/docs/http/configuring_https_servers.html#single_http_https_server

Nginx - Redirecting a server_name to a different domain using location block

What I would want is for me to type in test.com/path and have it go to anothersite.com, but instead, it goes to differentsite.com. I believe I'm doing the correct formatting, but seems to be skipping over the location block entirely.
server {
listen 80;
listen [::]:80;
server_name test.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443;
listen [::]:443;
server_name test.com;
location /path {
return 301 https://anothersite.com;
}
return 301 https://differentsite.com;
}
It turns out that the server block should not have a return statement. There should be two location blocks with return statements for each path
server {
listen 80;
listen [::]:80;
server_name test.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443;
listen [::]:443;
server_name test.com;
location / {
return 301 https://differentsite.com;
}
location /path {
return 301 https://anothersite.com;
}
}

Is it possible to split a server config into small ones in NGINX?

Having a virtualserver with lots of location directives in Nginx, is it possible to separate them into several files?
Example:
onefile.conf
server {
listen 80;
server_name domain.com;
location 1 { ... }
location 2 { ... }
location 3 { ... }
}
Change to:
server.conf
server {
listen 80;
server_name domain.com;
include server.location1.conf
include server.location2.conf
include server.location3.conf
}
server.location1.conf
server {
listen 80;
server_name domain.com;
location 1 { ... }
}
server.location2.conf
server {
listen 80;
server_name domain.com;
location 2 { ... }
}
server.location3.conf
server {
listen 80;
server_name domain.com;
location 3 { ... }
}

Resources