![真的很纠结于 nginx 重写为 https](https://linux22.com/image/645937/%E7%9C%9F%E7%9A%84%E5%BE%88%E7%BA%A0%E7%BB%93%E4%BA%8E%20nginx%20%E9%87%8D%E5%86%99%E4%B8%BA%20https%20.png)
我希望有人能帮帮忙。
我是 ubuntu 和 nginx 的新手,已经建立了自己的服务器(在网上各种指南的帮助下)
这是我的设置。
ubuntu 服务器 14.04、nginx、php5 fpm、静态 IP、默认工作网站(我觉得不需要设置虚拟主机)
请记住,几周前我对 nginx 一无所知,甚至不知道它的存在,所以我做得还不错,并且在此过程中学到了很多东西。
我想要的是可以从 www 访问
我的 IP 212.100.10.100(用于示例)
我的域名https://example.com
WWW 版本https://www.example.com
我已正确设置 SSL 证书,并且 nginx 对此很满意
我尝试将所有流量重定向到 https://,但结果好坏参半。IE 要么我访问我的网站并下载页面,要么出现错误 403,我认为表示请求正在发送到 https 端口。当发生这种情况时,我可以使用 https 顺利访问我的网站
这是我编辑过的 nginx 网站配置,请忽略#我一直在尝试一些东西 :) 目前我的网站在 http:// 上使用此配置运行良好,我也可以通过我的 IP 访问
server {
listen 80;
listen [::]:80 ipv6only=on;
server_name 212.100.10.100 example.com www.example.com;
# return 301 https:// $example.com$request_uri;
root /var/www/public_html;
index index.php index.html index.htm;
#}
#server {
# listen 80;
# server_name www.example.com
# return 301 https:// $example.com$request_uri;
# root /var/www/public_html;
# index index.php index.html index.htm;
#}
#server {
# listen 443 ssl;
# listen [::]:443 ipv6only=on;
# server_name example.com;
# root /var/www/public_html;
# index index.php index.html index.htm;
######HTTPS..
# ssl on;
# ssl_certificate /etc/nginx/ssl/bundle.cer;
# ssl_certificate_key /etc/nginx/ssl/example.key;
# ssl_session_timeout 5m;
# ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
# Add perfect forward secrecy
# ssl_prefer_server_ciphers on;
# ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA';
# Add HSTS
# add_header Strict-Transport-Security "max-age=31536000; includeSubdomains";
# Basic web server configuration.
index index.php
#access_log off;
client_max_body_size 1G;
# GZIP static content not processed by IPB.
gzip on;
gzip_static on;
gzip_http_version 1.1;
gzip_vary on;
gzip_comp_level 3;
gzip_proxied any;
gzip_types text/plain text/css application/json application/x-javascript ap$
gzip_buffers 16 8k;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
# Set up rewrite rules.
location / {
try_files $uri $uri/ /index.php;
}
location ~^(/page/).*(\.php)$ {
try_files $uri $uri/ /index.php;
}
# Stub Status module
location /server_status {
stub_status on;
#allow 127.0.0.1;
#deny all;
}
# Deny access to hidden files
location ~ /\. {
deny all;
}
# IP.Board PHP/CGI Protection
location ~^(/uploads/).*(\.php)$ {
deny all;
}
location ~^(/hooks/).*(\.php)$ {
deny all;
}
location ~^(/cache/).*(\.php)$ {
deny all;
}
location ~^(/screenshots/).*(\.php)$ {
deny all;
}
location ~^(/downloads/).*(\.php)$ {
deny all;
}
location ~^(/blog/).*(\.php)$ {
deny all;
}
location ~^(/public/style_).*(\.php)$ {
deny all;
}
# Caching directives for static files.
location ~^(/uploads/profile/).*\.(jpg|jpeg|gif|png)$ {
access_log off;
expires 1d;
}
location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|xml|htm|txt|swf|cur)$ {
access_log off;
expires 1w;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
try_files $uri $uri/ /index.php;
# With php5-cgi alone:
#fastcgi_pass 127.0.0.1:9000;
# With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_buffers 16 8k;
fastcgi_buffer_size 16k;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location /phpmyadmin {
auth_basic "Admin Login";
auth_basic_user_file /etc/nginx/pma_pass;
fastcgi_buffer_size 128k;
fastcgi_buffers 256 4k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
fastcgi_read_timeout 240;
}
###HTTPS REWRITES
#
# Rewrite http requests to https
#
#server {
# listen 80;
# server_name example.com;
# return 301 https:// example.com$request_uri;
#}
#
# Rewrite requests for http:// www.example.com to https:// example.com
#
#server {
# listen 80;
# server_name www.example.com;
# return 301 https:// example.com$request_uri;
#}
}
当我尝试配置它时,每次都会遇到问题,似乎如果我在服务器名称指令中没有 IP 地址和域,那么当我尝试访问它们时,我会得到标准的“欢迎使用 nginx”页面,这表明可能是我遗漏了另一个配置
编辑..
谢谢您的帮助,这是新的设置,它似乎可以起作用。
当我尝试让它正常工作时,我只是发表评论以保持我的网站在线。
再次感谢:) 如果有人发现我做错了什么我会很高兴听到。
server {
listen 80;
listen [::]:80 ipv6only=on;
server_name _;
return 301 https:// example.com$request_uri;
}
server {
listen 443 ssl;
listen [::]:443 ipv6only=on;
server_name example.com www.example.com;
root /var/www/public_html;
index index.php index.html index.htm;
######HTTPS..
ssl on;
ssl_certificate /etc/nginx/ssl/bundle.cer;
ssl_certificate_key /etc/nginx/ssl/example.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
# Add perfect forward secrecy
ssl_prefer_server_ciphers on;
ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA';
# Add HSTS
add_header Strict-Transport-Security "max-age=31536000; includeSubdomains";
# Basic web server configuration.
index index.php
#access_log off;
client_max_body_size 1G;
# GZIP static content not processed by IPB.
gzip on;
gzip_static on;
gzip_http_version 1.1;
gzip_vary on;
gzip_comp_level 3;
gzip_proxied any;
gzip_types text/plain text/css application/json application/x-javascript ap$
gzip_buffers 16 8k;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
# Set up rewrite rules.
location / {
try_files $uri $uri/ /index.php;
}
location ~^(/page/).*(\.php)$ {
try_files $uri $uri/ /index.php;
}
# Stub Status module
location /server_status {
stub_status on;
#allow 127.0.0.1;
#deny all;
}
# Deny access to hidden files
location ~ /\. {
deny all;
}
# IP.Board PHP/CGI Protection
location ~^(/uploads/).*(\.php)$ {
deny all;
}
location ~^(/hooks/).*(\.php)$ {
deny all;
}
location ~^(/cache/).*(\.php)$ {
deny all;
}
location ~^(/screenshots/).*(\.php)$ {
deny all;
}
location ~^(/downloads/).*(\.php)$ {
deny all;
}
location ~^(/blog/).*(\.php)$ {
deny all;
}
location ~^(/public/style_).*(\.php)$ {
deny all;
}
# Caching directives for static files.
location ~^(/uploads/profile/).*\.(jpg|jpeg|gif|png)$ {
access_log off;
expires 1d;
}
location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|xml|htm|txt|swf|cur)$ {
access_log off;
expires 1w;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
try_files $uri $uri/ /index.php;
# With php5-cgi alone:
#fastcgi_pass 127.0.0.1:9000;
# With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_buffers 16 8k;
fastcgi_buffer_size 16k;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location /phpmyadmin {
auth_basic "Admin Login";
auth_basic_user_file /etc/nginx/pma_pass;
fastcgi_buffer_size 128k;
fastcgi_buffers 256 4k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
fastcgi_read_timeout 240;
}
}
答案1
据我所知,您的 nginx 不监听 433/ssl。您的配置缺少实际的 http 到 https 重定向。使用 ip 地址server_name
是无稽之谈。
您应该创建两个server {}
语句,一个用于端口 80,另一个用于 ssl;并从第一个重定向到第二个。您的配置包含此类尝试的痕迹;我不明白您为什么注释掉所有这些。您唯一错过的(如果我们考虑您注释掉的部分,除了最后两个server {}
,它们是冗余和错误的)是实际的重定向,您可以从最后两个中的任何一个中获取它server {}
。