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

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

Related

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

nginx return 200 at other server_name

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

Nginx will rewirte image domain http to https, But I don't want the static server as https

I have a wordpress website with https protocol by configuring the nginx 301 redirect:
server {
listen 80;
server_name xxx.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name xxx.com;
ssl_certificate conf.d/xxx.crt;
ssl_certificate_key conf.d/xxx.key;
}
And my article has some image links with static server like:
http://yyy.com/1.png
But when i access this article: it will be https://yyy.com/1.png, How do I configure the nginx that can still use http for the image static server?
You would do that using below config
server {
listen 80;
server_name xxx.com;
location ~* \.(png|ico|jpeg)$ {
root <your root folder>;
try_files $uri =404;
}
location / {
return 301 https://$server_name$request_uri;
}
}
server {
listen 443 ssl;
server_name xxx.com;
ssl_certificate conf.d/xxx.crt;
ssl_certificate_key conf.d/xxx.key;
}

Redirect https:// to https://www

I have a ubuntu server with DigitalOcean which is managed using forge.laravel.com
I have tried numerous different methods in the nginx.conf file to redirect all the possible scenarios to my domain https://www.domain.com
https://www.domain.com = https://www.domain.com -- GREAT
http://domain.com = https://www.domain.com -- GREAT
http://www.domain.com = https://www.domain.com -- GREAT
https://domain.com = https://domain.com -- This should redirect to the www.
heres part of my nginx.conf
server {
listen 80;
server_name domain.com;
return 301 https://www.domain.com$request_uri;
}
server {
listen 80;
server_name www.domain.com;
return 301 https://www.domain.com$request_uri;
}
server {
server_name domain.com;
return 301 $scheme://www.domain.com$request_uri;
}
server {
listen 443 ssl;
server_name domain.com;
root /home/forge/default/public;
.... other nginx.conf configuration
Can anyone tell me what I'm doing wrong? I've tried numerous combinations.
Replace
server {
server_name domain.com;
return 301 $scheme://www.domain.com$request_uri;
}
server {
listen 443 ssl;
server_name domain.com;
root /home/forge/default/public;
By
server {
listen 443 ssl;
server_name domain.com;
return 301 https://www.domain.com$request_uri;
#INSERT HERE CERTIFICATES DIRECTIVES
}
server {
listen 443 ssl;
server_name www.domain.com;
root /home/forge/default/public;

How can I define conditional subdomains in nginx config file?

How can I do (if-else) condition for subdomains?
first check >> abc.example.com
second check >> xyz.example.com
...
others (case else:) >> *.example.com (This part is important for me, I cannot do that)
server {
listen 80;
server_name abc.example.com;
...
}
server {
listen 80;
server_name xyz.example.com;
...
}
server {
listen 80;
server_name *.example.com;
...
}

Resources