一、下载nginx

http://nginx.org/en/download.html

二、安装nginx

nginx安装可自行查阅,网上教程有很多

三、nginx的配置文件nginx.conf

找到#nginx安装目录/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 80;

server_name localhost;

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

#}

}

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

# }

#}

}

四、 配置nginx支持代理https

1).先执行nginx -V一定大写V查看对https的支持         看到输出,这个表示当前你的配置支持什么协议。当前是没有内容,也就是默认只支持HTTP。这个时候就要给起配置增加https的支持 2).https支持配置         重新编译安装nginx         进入源码目录就是解压的那个目录 3).重新配置执行以下命令         ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module         出现上面的错误就说需要Openssl的包。这个是对https依赖的软件,后面我们生成证书也需要它这个安装就不在这儿说了网上有教程。完事后再次执行上面的命令。在线安装的话执行yum -y install openssl openssl-devel即可。 4).上面执行成功后再次编译执行make         千万不要make install 会覆盖你之前安装的nginx 或者你的之前的编译的不想要了的话也可以执行make install 5).复制当前目录下的objs下的nginx到你的安装目录下。这个就是刚才编译好的 6).如果之前的nginx需要备份的话执行         cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak 7).接下来替换新的cp nginx /usr/local/nginx/sbin/ 8).在次执行nginx –V         此时就可以看到nginx既支持HTTP又支持https啦

五、 使用openssl生成秘钥和证书。

openssl安装配置可自行查阅,这里不在多说         我们都知道https协议是安全协议,访问的时候需要安全证书,让浏览器信任,当然公网上的是需要花钱买的,认证后所有浏览器都可以信任,我们的当然是私自生成的,具体的为什么大家可以去自己研究,我也不是很懂,知道个大概就行了。

一、生成CA私钥

mkdir ca

cd ca

#创建私钥 (建议设置密码)

openssl genrsa -des3 -out myCA.key 2048

#生成CA证书

# 20 年有效期

openssl req -x509 -new -nodes -key myCA.key -sha256 -days 7300 -out myCA.crt

# 查看证书信息命令

openssl x509 -in myCA.crt -noout -text

二、创建ssl证书私钥

cd ..

# 此文件夹存放待签名的证书

mkdir certs

cd certs

openssl genrsa -out localhost.key 2048

创建ssl证书CSR

openssl req -new -key localhost.key -out localhost.csr

创建域名附加配置文件cert.ext

authorityKeyIdentifier=keyid,issuer

basicConstraints=CA:FALSE

keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment

subjectAltName = @alt_names

[alt_names]

DNS.1 = localhost

IP.2 = 192.168.1.121

# ssl证书有效期10年,此步骤需要输入CA私钥的密码

openssl x509 -req -in localhost.csr -out localhost.crt -days 3650 -CAcreateserial -CA ../myCA.crt -CAkey ../myCA.key -CAserial serial -extfile cert.ext

三、生成pfx格式证书

openssl pkcs12 -export -out client.pfx -inkey localhost.key -in localhost.crt

六、 修改nginx配置

1、支持https

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

# }

#}

server {

listen 443 ssl;

server_name 192.168.1.121;

ssl_certificate # 改为自己申请得到的 crt 文件路径;

ssl_certificate_key # 改为自己申请得到的 key 文件路径;

ssl_session_cache shared:SSL:1m;

ssl_session_timeout 5m;

ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;

ssl_prefer_server_ciphers on;

}

这样https就配置成功了。访问时通过https://192.168.1.121:443就可以啦

2、支持websocket

只需要在location的配置中加上一下内容。监听端口什么的都不变

server {

listen 443 ssl;

server_name 192.168.1.121;

ssl_certificate # 改为自己申请得到的 crt 文件路径;

ssl_certificate_key # 改为自己申请得到的 key 文件路径;

ssl_session_cache shared:SSL:1m;

ssl_session_timeout 5m;

ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;

ssl_prefer_server_ciphers on;

location / {

proxy_pass https://192.168.1.121:6600/;

proxy_http_version 1.1;#协议版本。这儿必须写成这样

proxy_set_header Upgrade $http_upgrade;#

proxy_set_header Connection "upgrade";

proxy_read_timeout 600s;#连接保持时常,600s内没有消息换发则连接断开

}

}

        因为websocket是HTTP协议的升级版本,所以只要加上上面的即可,我们可以看大加了两个header 这是将协议转化为websocket。但是HTTP是协议版本必须是1.1至于为什么大家可以去查。所以上面的配置的意思就是当有websocket的请求进来时它将自动转换协议 访问时就是这样wss://localhost:443/webrtc 切记它的监听端口还是上述的HTTP端口

七、 nginx的重新加载

当你的nginx代理的资源发生变化时一定要重新加载,这个时候执行 nginx –s reload cd /user/local/nginx/sbin ./nginx –s reload

八、 注意

        nginx 首次启动时可能指定的配置文件不是我们之前配置的那个文件 这个时候有可能你会发现你无论你怎么修改你的配置文件就访问不了这个时候就要指定配置文件启动了 /usr/local/nginx/sbin/nginx -s reload -c /usr/local/nginx/conf/nginx.conf

相关文章

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。