[英]Forcing www and https redirect on NGINX as backend of Google cloud Load Balancer using $http_x_forwarded_proto
I have a NGINX backend instance for the google cloud platform load balancer. 我有一个针对谷歌云平台负载均衡器的NGINX后端实例。 I have and have successfully set up the https redirect.
我已经成功设置了https重定向。 I want to force from non-www to www.
我想强制从非www到www。 For the most part all but one instance redirects to a www url.
在大多数情况下,除一个实例之外的所有实例都重定向到www网址。 to Summarize:
总结一下:
https://ampkart.com not successful and stays as https://ampkart.com https://ampkart.com未成功并保持为https://ampkart.com
I have tried multiple edits to the redirect in the default.conf file to force it but to no avail. 我已尝试对default.conf文件中的重定向进行多次编辑以强制它,但无济于事。 I know it probably has something to 'http' in the bracket and it is only affecting the http urls to redirect to www but the https urls are not affected.
我知道它可能在括号中有'http'的东西,它只影响http网址重定向到www,但https网址不受影响。 Can someone please help me modify this code for the redirect to work?
有人可以帮我修改此代码以使重定向工作吗? Here is my default.conf file for my NGINX server.
这是我的NGINX服务器的default.conf文件。
server {
listen 80 default_server;
listen [::]:80 default_server;
# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;
root /storage;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;
server_name ampkart.com;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
error_page 405 =200 $uri;
}
# pass PHP scripts to FastCGI server
#
location ~ \.php$ {
if ($http_x_forwarded_proto = 'http'){
rewrite ^/(.*)$ https://www.ampkart.com/$1 permanent;
}
include snippets/fastcgi-php.conf;
# # With php-fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
# # With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
if ($request_method ~* "(GET|POST|OPTIONS)") {
add_header "Access-Control-Allow-Methods" "GET, POST,
OPTIONS";
add_header "Access-Control-Allow-Credentials" "true";
add_header "Access-Control-Allow-Origin"
"https://www.ampkart.com";
add_header "Access-Control-Expose-Headers" "AMP-
Access-Control-Allow-Source-Origin,Content-Length,Content-Range";
add_header 'Access-Control-Allow-Headers' 'DNT,User-
Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-
Type,Range';
add_header 'AMP-Access-Control-Allow-Source-Origin'
"https://www.ampkart.com";
add_header "AMP-Same-Origin" "true";
add_header "AMP-Redirect-to"
"https://www.ampkart.com$request_uri";
}
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
I would like to be able to redirect https://ampkartcom to https://www.ampkart.com 我希望能够将https:// ampkartcom重定向到https://www.ampkart.com
Solved. 解决了。 If you are trying to implement forced https and www or non-www with a nginx backend of Google cloud load balancer, just use the cold below: Place it underneath the server_name, change example.com to your domain.
如果您尝试使用Google云负载均衡器的nginx后端实施强制https和www或非www,请使用以下内容:将其放在server_name下面,将example.com更改为您的域。
if ($http_host ~* '^example.com'){
rewrite ^(.*)$ https://www.example.com$request_uri redirect; }
this will force https with the nginx backend and if you don't want the www and want to force non-www, just remove the "www." 这将迫使https与nginx后端,如果你不想要www并想强制非www,只需删除“www。”
Also, here is a full default.conf for my website nginx backend of the google cloud load balancer with php-fpm enabled. 此外,这是一个完整的default.conf,用于我的网站nginx后端的谷歌云负载均衡器启用php-fpm。 Enjoy!
请享用!
##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# https://www.nginx.com/resources/wiki/start/
# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
# https://wiki.debian.org/Nginx/DirectoryStructure
#
# In most cases, administrators will remove this file from sites-enabled/ and
# leave it as reference inside of sites-available where it will continue to be
# updated by the nginx packaging team.
#
# This file will automatically load configuration files provided by other
# applications, such as Drupal or Wordpress. These applications will be made
# available underneath a path with that package name, such as /drupal8.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##
# Default server configuration
#
server {
listen 80 default_server;
listen [::]:80 default_server;
# SSL configuration
#
#listen 443 ssl default_server;
#listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;
root /www;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;
server_name www.example.com;
if ($http_host ~* '^example.com'){
rewrite ^(.*)$ https://www.example.com$request_uri redirect; }
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
error_page 405 =200 $uri;
}
# pass PHP scripts to FastCGI server
#
location ~ \.php$ {
include snippets/fastcgi-php.conf;
# # With php-fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
# # With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
# listen 80;
# listen [::]:80;
#
# server_name example.com;
#
# root /var/www/example.com;
# index index.html;
#
# location / {
# try_files $uri $uri/ =404;
# }
#}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.