您的位置:首页 > 理论基础 > 计算机网络

centos+django+uwsgi+nginx配置ssl访问https服务器和网址

2019-06-09 12:55 841 查看

前提:uwsgi已经安装并且用外网IP可以访问
已经获得网址并通过备案,并且已经解析,如果要配置SSL需要申请SSL证书
目的:配置网址与服务器IP关联,实现网址访问服务器

nginx 安装和卸载及其他命令:

#安装nginx,安装位置是/etc/nginx/
sudo yum install nginx
#卸载yum安装得nginx
sudo yum remove nginx
#nginx测试
nginx -t
#nginx的启动和停止
./nginx -s reload
restart  nginx.service
service nginx restart
service nginx stop
service nginx start
pkill -9 nginx
#查看端口占用情况
netstat -lnpt | grep 8000

首先修改uwsgi.ini中的配置:

[uwsgi]
# django项目监听的socket文件(可以使用端口代替)
#socket =/usr/local/src/catclass/catclass/catclass.sock
# django项目所在目录
chdir = /usr/local/src/catclass/
# django项目wsgi文件
wsgi-file = /项目文件夹/wsgi.py
module =项目名称.wsgi
#http是浏览器协议,用作外网IP访问的端口设置;socket是nginx协议,用作与nginx通讯的本地端口设置
socket = 127.0.0.1:8000
http=0.0.0.0:666
# 指定静态文件
static-map=/static=/usr/local/src/项目名称/static
# 启动uwsgi的用户名和用户组
uid=root
gid=root
# 启用主进程
master = true
processes = 2
threads = 4
# 自动移除unix Socket和pid文件当服务停止的时候
vacuum = true
# 序列化接受的内容,如果可能的话
thunder-lock=true
# 启用线程
enable-threads=true
# 设置自中断时间
harakiri=30
# 设置缓冲
post-buffering=4096
# 通过touch reload可以重启uwsgi服务器
touch-reload = ./reload
# 日志输出
daemonize = catclass.log

在/etc/nginx中新建conf.d文件夹(如果没有):

mkdir conf.d

在conf.d中新建.conf文件,文件名可以自己命名

vim https.conf

在.conf文件中配置如下内容

#the upstream component nginx needs to connect to
#定义location中的django字段,为uwsgi.ini中的socket =后面的服务器本地访问端口,是uwsgi和nginx连接的端口
upstream django {
# server unix:///path/to/your/mysite/mysite.sock; # for a file socket
server 127.0.0.1:8000; # for a web port socket (we'll use this first)
}
# configuration of the server

server {
# the port your site will be served on网址默认的访问端口
listen      80;
# the domain name it will serve for已经备案和解析好的网址
server_name xxxx.cn; # substitute your machine's IP address or FQDN
charset     utf-8;

# max upload size
client_max_body_size 75M;   # adjust to taste

# Django media
location /media  {
alias /usr/local/src/项目/media;  # 指向django的media目录
}

location /static {
alias /usr/local/src/项目/static; # 指向django的static目录
}

# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass  django;#django是最开始定义的字段,为与uwsgi通信的本地端口
include     uwsgi_params;
}
}

#以下配置ssl和https
server{
#比起默认的80 使用了443 默认 是ssl方式  多出default之后的ssl
listen 443;
#开启  如果把ssl on;这行去掉,ssl写在443端口后面。这样http和https的链>接都可以用
ssl on;
#证书(公钥.发送到客户端的)
ssl_certificate /usr/local/ssl_key/xxxx.cn.crt;
#私钥,
ssl_certificate_key /usr/local/ssl_key/xxxx.cn.key;
#下面是绑定域名
server_name xxxxx.cn;

location / {
uwsgi_pass 127.0.0.1:8000;
include uwsgi_params;
#proxy_redirect off; #禁止跳转
#proxy_set_header Host $host;
#proxy_set_header X-Real-IP $remote_addr;
#proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#配置反向代理
#proxy_pass http://127.0.0.1:8000;
}

#access_log /data/log/nginx/mysite.access.log;
}

修改/etc/nginx中的nginx.conf中的user为root

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐