nginx配置及相关问题连接————个人记录

一、NGINX部署HTTPS(第一部分转载自下面的文章)

https://cloud.tencent.com/developer/article/1157556

 

Nginx 的中文维基:http://wiki.codemongers.com/NginxChs

Nginx 的中文站: http://www.nginx.cn

 

 部署服务器

安装nginx

apt install nginx -y

使用如下命令确认是否支持HTTPSSNI

root@VM-171-28-ubuntu:/# nginx -V
nginx version: nginx/1.10.3 (Ubuntu)
built with OpenSSL 1.0.2g  1 Mar 2016
TLS SNI support enabled
configure arguments: --with-cc-opt='-g -O2 -fPIE -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -fPIE -pie -Wl,-z,relro -Wl,-z,now' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-ipv6 --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_addition_module --with-http_dav_module --with-http_geoip_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_v2_module --with-http_sub_module --with-http_xslt_module --with-stream --with-stream_ssl_module --with-mail --with-mail_ssl_module --with-threads

TLS SNI support enabled表示SNI已经启用;--with-http_ssl_module表示支持HTTPS

创建配置文件(跟原文不一样,原博客上面的连接总是出各种错,下面的conf文件是官网上的)

将证书文件和私钥文件放到/etc/nginx/ssl目录下。在/etc/nginx/conf.d目录下创建mysite.conf文件。内容如下:

server {
        #侦听80端口
        listen    80;
        #定义使用 www.nginx.cn访问
        server_name  www.nginx.cn;#这里要改
 
        #定义服务器的默认网站根目录位置
        root html;
 
        #设定本虚拟主机的访问日志
        access_log  logs/nginx.access.log;#这里要改


        ssl on;
        #证书(公钥.发送到客户端的)
        ssl_certificate /etc/nginx/ssl/mysite.crt;
        #私钥,
        ssl_certificate_key /etc/nginx/ssl/mysite.key;
    
 
        #默认请求
        location / {
            
            #定义首页索引文件的名称
            index index.php index.html index.htm;   
 
        }
 
        # 定义错误提示页面
        error_page   500 502 503 504 /50x.html;
        location = /50x.html {
        }
 
        #静态文件,nginx自己处理
        location ~ ^/(images|javascript|js|css|flash|media|static)/ {
            
            #过期30天,静态文件不怎么更新,过期可以设大一点,
            #如果频繁更新,则可以设置得小一点。
            expires 30d;
        }
 
        #PHP 脚本请求全部转发到 FastCGI处理. 使用FastCGI默认配置.
        location ~ .php$ {
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
 
        #禁止访问 .htxxx 文件
            location ~ /.ht {
            deny all;
        }
 
    }

前两个server配置项是为了已配置域名使用HTTP访问时,强制跳转到HTTPS;ip或未配置域名访问时,返回400错误。

第三个server配置项中使用了default.crt证书,这是个自签名证书,是为了当用户通过ip或未配置域名使用HTTPS访问时,能够返回400错误。本来是想做成SSL握手时只要发现没有传SNI或不支持的域名时,就直接断开连接。但是,发现貌似nginx不支持这一特性(参考此链接),只能使用这种折中的解决方法。

重启nginx

systemctl restart nginx

如果没有报错的话,HTTPS服务就可以正常访问了。

0x03 后记

使用nginx反向代理的优点是:可以使用nginx实现HTTPS,而自己的Web服务使用HTTP。这样,开发、调试都很方便,也便于使用负载均衡。

 

 

二、查看Nginx相关命令

#查看Nginx状态
1.systemctl status nginx 

2.netstat -tnlp 

3.ps -ef | grep nginx

4.pkill -9 被占用的端口号(如23890)

#启动Nginx
5.systemctl restart nginx

#关闭
6.systemctl stop nginx


官网:
sudo /usr/local/nginx/nginx     (nginx二进制文件绝对路径,可以根据自己安装路径实际决定)
 
nginx从容停止命令,等所有请求结束后关闭服务
ps -ef |grep nginx
kill -QUIT  nginx主进程号
 
nginx 快速停止命令,立刻关闭nginx进程
ps -ef |grep nginx
kill -TERM nginx主进程号 
如果以上命令不管用,可以强制停止
kill -9 nginx主进程号

 若系统没有systemctl(好像是ubuntu14后才有的),则先找出nginx可执行文件在哪(ps -ef | grep nginx),我的是在“/usr/sbin”目录下,然后“cd /usr/sbin”,具体的可以百度下命令

#重启
sudo ./nginx -s reload 

 三、问题补录:

3.1:js/css等资源不能加载之“nginxnet::ERR_ABORTED 403 (Forbidden)”

解决方法之一:在/etc/nginx/nginx.conf 这个文件夹的第一行修改user root(或者当前用户); 这个是由于启动nginx的用户和当前用户不一样。保存后重新启动nginx就可以了。

3.2:配置websock

在/etc/nginx/nginx.conf 添加,加密的时候,创建websocket用“wss”,没加密用“ws”,创建的时候记得要加路径,如“wss://www.zjyxxn.cn/api/”

map $http_upgrade $connection_upgrade {
    default upgrade;
    '' close;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值