Nginx+php+http/https伺服器配置
阿新 • • 發佈:2019-02-11
測試系統:ubantu 16.04
測試時間:2017-12-21
關於SSL證書說明
博主用的DV免費證書做的測試。
關於SSL Key 和 CSR 檔案或PEM檔案
一般SSL發行商會提供給大家key或者csr。
如果沒有,可以自己生成,用OpenSSL。
.key
是私鑰
配置HTTPS
首先得進入nginx主機配置目錄:/var/nginx/sites-enabled
**配置https和http有兩種方式:
- 一種是兩者共存
- 另一種是強制http跳轉https
同時監聽80和443埠
server {
listen 80;#同時開啟80和443埠
listen 443 ssl;
server_name example.com;
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
#證書檔案
ssl_certificate example.com.crt;
#私鑰檔案
ssl_certificate_key example.com.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
#php解析
location ~ \.php$ {
include snippets/fastcgi-php.conf;
#
# # With php7.0-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# # With php7.0-fpm:
fastcgi_pass unix:/run/php/php7.0 -fpm.sock;
}
}
強制http跳轉https
server {
listen 80;
server_name example.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name example.com;
ssl on;
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
ssl_certificate example.com.curl;
ssl_certificate_key example.com.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
location / {
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
include snippets/fastcgi-php.conf;
#
# # With php7.0-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# # With php7.0-fpm:
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
# deny .git access
location ~ /\.git {
deny all;
}
}
參考:址一