对于example.com的子域,我很难将nginx重定向到非www。
基本上我想要的是
下面的代码不起作用,并根据 nginx www redirect to non-www with subdomain 似乎应该起作用。
感谢所有帮助。请指教!
server {
listen 80;
server_name example.com *.example.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name api.example.com;
root /var/www/html.live/laravel/public;
index index.php index.html index.htm;
ssl_certificate /etc/ssl/wild_example.crt;
ssl_certificate_key /etc/ssl/wild_example.crt;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
add_header 'Access-Control-Allow-Origin' "$http_origin" always;
add_header "Vary" "Origin";
add_header 'Access-Control-Allow-Methods' 'POST, GET, OPTIONS, DELETE, PUT';
add_header 'Access-Control-Allow-Headers' 'Origin, Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With, MBM-User-Token, X-CSRF-TOKEN';
add_header 'Access-Control-Allow-Credentials' 'true';
access_log /var/log/nginx/api_access.log;
error_log /var/log/nginx/api_error.log warn;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php-fpm/php70_www.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
server {
listen 443 ssl;
server_name ~^www\.(?<site_id>.+)\.example\.com$;
return 301 $scheme://$site_id.example.com$request_uri;
}
# CAKE 443
server {
listen 443 ssl;
server_name *.example.com;
root /var/www/html.live/app/webroot/;
index index.php;
client_max_body_size 32M;
ssl_certificate /etc/ssl/wild_example.crt;
ssl_certificate_key /etc/ssl/wild_example.crt;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
access_log /var/log/nginx/main_access.log main_ext;
error_log /var/log/nginx/main_error.log warn;
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'POST, GET, OPTIONS, DELETE, PUT';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
location / {
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'Content-Length' 0;
return 204;
}
try_files $uri $uri/ /index.php?$args;
}
if (!-e $request_filename) {
rewrite ^(.+)$ /index.php?q=$1 last;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/run/php-fpm/php56_www.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param origin_is $host;
include /etc/nginx/fastcgi_params;
}
}
server {
listen 443 ssl;
server_name example.com;
root /var/www/html.live/app/webroot/;
index index.php;
client_max_body_size 32M;
ssl_certificate /etc/ssl/wild_example.crt;
ssl_certificate_key /etc/ssl/wild_example.crt;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
access_log /var/log/nginx/main_access.log main_ext;
error_log /var/log/nginx/main_error.log warn;
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'POST, GET, OPTIONS, DELETE, PUT';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
location / {
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'Content-Length' 0;
return 204;
}
try_files $uri $uri/ /index.php?$args;
}
if (!-e $request_filename) {
rewrite ^(.+)$ /index.php?q=$1 last;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/run/php-fpm/php56_www.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param origin_is $host;
include /etc/nginx/fastcgi_params;
}
}