首页 服务器技术 nginx

Nginx安装并开启https

本文以CentOS为例,安装Nginx,并开启HTTPS。

  1. 卸载原有的openssl,查看openssl安装情况,系统自带的需要卸载。


查看已安装情况

rpm -qa | grep openssl

openssl-libs-1.0.2k-8.el7.x86_64

openssl-1.0.2k-8.el7.x86_64

openssl-devel-1.0.2k-8.el7.x86_64

强制卸载

rpm -e --nodeps openssl-libs-1.0.2k-8.el7.x86_64 openssl-1.0.2k-8.el7.x86_64 openssl-devel-1.0.2k-8.el7.x86_64

2.安装openssl

这里使用的是挂在的本地yum源中的rpm文件进行的安装

cd /media/cdrom/Packages/

rpm -ivh openssl-libs-1.0.2k-8.el7.x86_64.rpm openssl-devel-1.0.2k-8.el7.x86_64.rpm openssl-1.0.2k-8.el7.x86_64.rpm

检查安装情况 openssl version -a

出现如图所示,即正确

3.安装nginx,并增加https模块

进入nginx目录后,执行

./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

这里将nginx安装到/usr/local/nginx下,也可以安装到指定的其他目录。

./configure出错,要根据提示的错误来解决,这里就不详细写了。一般的错误类型,都是缺少依赖包。

我是使用的root安装的,所有make install可以直接提交,如果不是使用的root安装,make install 可能会出错,make install 不执行,就把objs下面的额nginx 复制到 sbin 下。

安装完成后看下安装情况。

生成证书:

openssl req -x509 -nodes -days 36500 -newkey rsa:2048 -keyout /data/nginx/ssl/nginx.key -out /data/nginx/ssl/nginx.crt


修改 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;

upstream fz{

ip_hash;

server 192.168.1.1:8888 max_fails=1 fail_timeout=10s;

server 192.168.1.2:8888 max_fails=1 fail_timeout=10s;

}

server {

listen 8888;

server_name 192.168.1.100;

ssl_certificate /data/ssl/nginx.crt;

ssl_certificate_key /data/ssl/nginx.key;

ssl_session_cache shared:SSL:1m;

ssl_session_timeout 5m;

server_tokens off;

fastcgi_param HTTPS on;

fastcgi_param HTTP_SCHEME https;

access_log /data/nginx/logs/httpsaccess.log;

#charset koi8-r;

#access_log logs/host.access.log main;

location / {

proxy_pass http://fz;

}

#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;

#}

}

# 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 8888 ssl;

server_name _;

rewrite ^(.*) https://$server_name$1 permanent;

}

}

将修改后的配置文件,放到conf目录下,

执行nginx -c
/usr/local/nginx/conf/nginx.conf 启动nginx

到这,nginx就配置完成啦。

相关推荐