I am trying to redirect all my traffic from http to https automatically. How can i do a 301 redirect to all my domain and subdomains?
This is NGINX Config file
upstream app_server {
server unix:/run/DigitalOceanOneClick/unicorn.sock fail_timeout=0;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name sprintsocial.io app.sprintsocial.io admin.sprintsocial.io;
# return 301 https://$server_name$request_uri;
}
server {
#listen 80;
listen 443;
root /home/rails/sprintsocial/public;
#server_name _;
server_name sprintsocial.io app.sprintsocial.io admin.sprintsocial.io;
ssl on;
ssl_certificate /home/sprintsocial.io.chained.crt;
ssl_certificate_key /home/sprintsocial.io.key;
index index.htm index.html;
# return 301 https://$server_name$request_uri;
# rewrite ^/(.*) https://app.sprintsocial.io/$1 permanent;
# rewrite ^/(.*) https://admin.sprintsocial.io/$1 permanent;
location / {
try_files $uri/index.html $uri.html $uri #app;
}
location ~* ^.+\.(jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|mp3|flv|mpeg|avi)$ {
try_files $uri #app;
}
location #app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app_server;
}
}
The default server will accept http connections for any server name (without an explicit server block). Use the $host variable to determine the name of the requested domain.
server {
listen 80 default_server;
listen [::]:80 default_server;
return 301 https://$host$request_uri;
}
See this document for more.
Related
I would like to redirect my website after entering www.lombo.pl to only lombo.pl (with SSL certificate).
Now when something write www.lombo.pl it does not redirect. I tried to change my nginx file but still to no avail. The user can visit my website via www.lombo.pl (which at the same time shows an error because I do not have a SSL certificate configured for this domain).
upstream app_server {
server unix:/home/app/run/gunicorn.sock fail_timeout=0;
}
server {
#listen 80;
# add here the ip address of your server
# or a domain pointing to that ip (like example.com or www.example.com)
listen 443 ssl;
server_name lombo.pl;
ssl_certificate /etc/letsencrypt/live/lombo.pl/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/lombo.pl/privkey.pem;
server_name 157.245.228.127;
keepalive_timeout 5;
client_max_body_size 4G;
access_log /home/app/logs/nginx-access.log;
error_log /home/app/logs/nginx-error.log;
location /static/ {
alias /home/app/static/;
}
# checks for static file, if not found proxy to app
location / {
try_files $uri #proxy_to_app;
}
location #proxy_to_app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app_server;
}
}
server {
listen 80;
server_name lombo.pl;
return 301 https://$host$request_uri;
}
server {
listen 80;
server_name www.lombo.pl;
return 301 https://$host$request_uri;
}
server {
listen 443;
server_name www.lombo.pl;
return 301 https://$host$request_uri;
}
I've tired to configure an Nginx server with SSL but the site is not open but with https:// it's open normally.
Here is my Nginx configuration:
# Default server configuration
#
server {
listen 80 default_server;
listen [::]:80 default_server;
client_max_body_size 20M;
root /var/www/mysite.in/site;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm;
server_name _;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
# pass PHP scripts to FastCGI server
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
}
listen 443;
listen [::]:443;
server_name www.mysite.com;
#ssl on;
ssl_certificate /etc/nginx/ssl/mysite.com.chained.crt;
ssl_certificate_key /etc/nginx/ssl/main_private.key;
}
The SSL was generated in GoDaddy, I've found lots of solutions, but so far none of them are working.
How can I resolve this error?
You have to create two servers(http and https) in your config and create redirect from http to https:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name www.mysite.com;
return 301 https://$host$request_uri;
}
server {
listen 443;
listen [::]:443;
server_name www.mysite.com;
client_max_body_size 20M;
root /var/www/mysite.in/site;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm;
server_name _;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
# pass PHP scripts to FastCGI server
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
}
ssl on;
ssl_certificate /etc/nginx/ssl/mysite.com.chained.crt;
ssl_certificate_key /etc/nginx/ssl/main_private.key;
}
I need to make HTTP->HTTPS redirection for whole site, but every time I get error message with 301 loop redirection. Please correct my conf to not get 301 error.Here is my conf file:
upstream live {
server IP:PORT;
}
server {
listen 80 default;
server_name mysite.com;
access_log off;
error_log off;
root /usr/share/nginx/html/;
index index.html index.htm;
return 301 https://$host$request_uri;
}
server {
listen 443;
root /usr/share/nginx/html/;
index index.html index.htm;
rewrite_log on;
server_name mysite.com;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log debug;
client_max_body_size 200m;
location / {
index index.html index.htm;
try_files $uri $uri/ /index.html?$uri&$args;
}
location /web {
proxy_pass https://live/gateway/web;
proxy_set_header "MP_FRONT" aaa;
proxy_pass_request_headers on;
}
}
Just try to replace first server JSON like below
server {
listen 80;
listen [::]:80 default_server ipv6only=on;
server_name example.com;
return 301 https://www.example.com$request_uri;
}
please, try and let me know its working for you or not
Instead of executing .php, it's downloading.
I am trying to configure php7 on Ubuntu 16.04 LTS and my /etc/nginx/sites-available/default looks like this.
Can anyone help?
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name mydomain.com www.mydomain.com;
return 301 https://$server_name$request_uri;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
include fastcgi_params;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
}
server {
index index.html index.htm index.nginx-debian.html;
# SSL configuration
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
include snippets/ssl-mydomain.com.conf;
include snippets/ssl-params.conf;
location /web {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_pass http://localhost:32400/web/;
}
}
It looks like your second server (ssl) block is missing the location ~ \.php$ block. This is what tells nginx to execute the PHP rather than serve it raw.
In other words, give this a go:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name mydomain.com www.mydomain.com;
return 301 https://$server_name$request_uri;
location / {
try_files $uri $uri/ =404;
}
}
server {
index index.html index.htm index.nginx-debian.html;
# SSL configuration
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
include snippets/ssl-mydomain.com.conf;
include snippets/ssl-params.conf;
location /web {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_pass http://localhost:32400/web/;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
include fastcgi_params;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
}
You redirect all to https and your https server part doesn't handle PHP, see missing fastcgi configs.
Here is my config:
upstream example {
server unix:///home/deploy/apps/example/shared/tmp/sockets/example-puma.sock;
}
server {
listen 80 default_server;
server_name example.org;
location /non_https {
proxy_pass http://example;
}
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl default_server;
ssl_certificate /etc/letsencrypt/live/example.org/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.org/privkey.pem;
include snippets/ssl-params.conf;
root /home/deploy/apps/example/current/public;
try_files $uri/index.html $uri #example;
location #example {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://example;
}
keepalive_timeout 10;
}
I need to turn off ssl for /non_https/* how to get it?
If you place the retuen 301 inside a location "/" block it should work:
server {
listen 80 default_server;
server_name example.org;
location /non_https {
proxy_pass http://example;
}
location / {
return 301 https://$server_name$request_uri;
}
}