Nginx在Linux下的安装
重新准备一台虚拟机作为服务器。比如IP地址为192.168.222.132
环境准备
(1)需要安装 gcc 的环境:
yum install gcc-c++
(2)第三方的开发包
PCRE
PCRE(Perl Compatible Regular Expressions)是一个 Perl 库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库。
yum install -y pcre pcre-devel
注:pcre-devel 是使用 pcre 开发的一个二次开发库。nginx 也需要此库。
zlib
zlib 库提供了很多种压缩和解压缩的方式,nginx 使用 zlib 对 http 包的内容进行 gzip,所以需要在 linux 上安装 zlib 库。
yum install -y zlib zlib-devel
OpenSSL
OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。nginx 不仅支持 http 协议,还支持 https(即在 ssl 协议上传输 http),所以需要在 linux安装 openssl 库。
yum install -y openssl openssl-devel
Nginx下载
官方网站下载 nginx:http://nginx.org/
我使用的版本是 1.8.0 版本
Nginx安装
第一步:把 nginx 的源码包nginx-1.8.0.tar.gz上传到 linux 系统
Alt+p 启动sftp ,将d盘setup文件夹中的nginx-1.8.0.tar.gz上传
输入命令:put d:/setup/nginx-1.8.0.tar.gz
第二步:解压缩
tar zxvf nginx-1.8.0.tar.gz
第三步:进入nginx-1.8.0目录 使用 configure 命令创建一 makeFile 文件。
./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-http_ssl_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi
\ 代表换行
执行后可以看到Makefile文件
第四步:编译
make
第五步:安装
make install
Nginx启动与访问
注意:启动nginx 之前,上边将临时文件目录指定为/var/temp/nginx/client, 需要在/var 下创建此 目录
mkdir /var/temp/nginx/client -p
进入到Nginx目录下的sbin目录
cd /usr/local/ngiux/sbin
输入命令启动Nginx
./nginx
启动后查看进程
ps aux|grep nginx
地址栏输入虚拟机的IP即可访问(默认为80端口)
配置https
1、生成证书步骤
cd /usr/local/nginx/conf
a、//生成私钥
openssl genrsa -des3 -out server.key 1024
密码输入:123456
继续输入123456
b、//生成证书请求文件
openssl req -new -key server.key -out server.csr
c、//生成证书(测试时可使用)
openssl req -x509 -days 3650 -key server.key -in server.csr > server.crt
server.key输入123456
说明:36500天表示一百年,这是用步骤a,b的的密钥和证书请求生成证书server.crt,-days参数指明证书有效期,单位为天,x509表示生成的为X.509证书。以上签署证书仅仅做测试用,真正运行的时候,应该将CSR发送到一个CA返回真正的证书。网上有些文档描述生成证书文件的过程比较繁琐,就是因为 他们自己建立了一个CA中心,然后再签署server.csr。
用openssl x509 -noout -text -in server.crt 可以查看证书的内容。证书实际上包含了Public Key
d、//生成无密的私钥(可避免nginx配置SSL安全证书后,重启免输入密码)
openssl rsa -in server.key -out server.key.unsecure
server.key输入123456
e、将crt证书格式导出为cer证书格式,供ios客服端调用使用
openssl x509 -in server.crt -out server.cer -outform der
nginx的配置:
这里使用EditPlus编辑nginx的配置文件。关于EditPlus怎么连接linux,不知道的小伙伴可以看下这个链接:https://blog.csdn.net/eagleuniversityeye/article/details/79998348
重启nginx
在浏览器中输入https://192.168.222.132
点击高级
https配置成功
一些工具使用:
1、查看证书日期
openssl x509 -in server.crt -noout -dates
以上说明证书有效期为100年
现在网站是升级https了,但是用户又不知道,而且不输入协议头的话浏览器默认都是按照http来加载,所以我们还要对http做301 自动跳转处理。
方式一:
在配置文件中增加一个虚拟主机:
重启nginx:
./nginx -s reload
在浏览器中输入192.168.222.132/
上述自动跳转配置会自动携带URL和参数,例如,访问 http://192.168.222.132/?a=1 会自动跳转到 http://192.168.222.132/?a=1。
方式二:
利用meta的刷新作用将http跳转到https-
上述的方法均会耗费服务器的资源,可以借鉴百度使用的方法:巧妙的利用meta的刷新作用,将http跳转到https,可以基于http://dev
.wangshibo.com的虚拟主机路径下写一个index.html,内容就是http向https的跳转
将下面的内容追加到index.html首页文件内
新建文件夹:/var/www/html/8080/
mkdir /var/www/html/8080/ -p
新建文件index.html
touch index.html
用EditPlus打开index.html
将以下代码拷贝到index.html中
<html>
<meta http-equiv="refresh" content="0;url=https://192.168.222.132/">
</html>
修改配置文件:
重启nginx
在浏览器中输入192.168.222.132。将会自动跳转到https://192.168.222.132
以上https的证书是我自己生成的,所有浏览器仍然会显示不安全字样。需要到正规渠道购买证书才行。
这里贴一下配置文件nginx.conf:
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 443 ssl;
server_name localhost;
charset utf-8;
ssl on;
ssl_certificate /usr/local/nginx/conf/server.crt;
ssl_certificate_key /usr/local/nginx/conf/server.key.unsecure;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
server {
listen 80;
server_name 192.168.222.132;
index index.html index.php index.htm;
access_log /usr/local/nginx/logs/8080-access.log;
error_log /usr/local/nginx/logs/8080-error.log;
#将404的页面重定向到https的首页
error_page 404 https://192.168.222.132/;
location ~ / {
root /var/www/html/8080;
index index.html index.php index.htm;
}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}