Django-31 nginx-1 安装 配置 MACOS 下的配置 启动/停止 修改uWSGI配置

  • nginx是轻量级的高性能Web服务器,提供了诸如HTTP代理和反向代理、负载均衡等一系列重要特性
  • C语言编写,执行效率高
  • nginx作用
    • 负载均衡,多台服务器轮流处理请求
    • 反向代理
  • 原理:
    • 客户端请求nginx,再由nginx将请求转发uWSGI运行的django

安装

sudo apt install nginx
我在macos下是执行brew install nginx安装成功的
如果下载速度很慢,考虑更换为国内源

vim /etc/apt/sources.list
更改国内源
sudo apt-get update

安装完毕后,ubuntu终端中输入nginx -v 显示如下:

nginx version: nginx/1.14.0 (Ubuntu)

macos输入显示如下

liujiadeMacBook-Pro:tedu_note liujia$ nginx -v
nginx version: nginx/1.13.12

配置

  • 修改nginx的配置文件/etc/nginx/sites-enabled/default;
    sudo vim该文件
# 在server节点下添加新的location项,指向uwsgi的ip与端口
server{
  ...
  location / {
    uwsgi_pass 127.0.0.1:8000; # 重定向到127.0.0.1的8000端口
    include /etc/nginx/uwsgi_params; #将所有的参数转到uwsgi下
  }
   ...
}

我的macos安装好后会自动监听8080端口,具体监听哪个端口是在/usr/local/etc/nginx/nginx.conf.default 里面找到的,直接改改不了,要vim去修改


MACOS 下的配置

➜  ~ cd Downloads
➜  wget http://nginx.org/download/nginx-1.13.4.tar.gz
➜  tar xvzf nginx-1.13.4.tar.gz
➜  cd nginx-1.13.4
➜  sudo ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-cc-opt="-Wno-deprecated-declarations"
➜  sudo make
➜  sudo make install
➜  cd /usr/local/nginx/conf
➜  conf sudo mkdir conf.d
➜  conf sudo nano nginx.conf
// 在最后}前加入一行
    include ./conf.d/*.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;
    #    }
    #}
    include ./conf.d/*.conf;
    #include /etc/nginx/conf.d/*.conf;
}

➜ /etc sudo nginx #启动nginx
➜ /etc sudo nginx -s reload #重新加载nginx配置
➜ /etc sudo nginx -s stop #停止nginx

检测配置文件的语法是否有问题

liujiadeMacBook-Pro:nginx liujia$ sudo nginx -t
nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful

启动/停止

$ sudo /etc/init.d/nginx start|stop|restart|status

$ sudo service nginx start|stop|restart|status

注意:nginx配置只要修改,就需要进行重启,否则配置不生效

修改uWSGI配置

nginx负责接收请求,并把请求转发给后面的uWSGI
此模式下,uWSGI需要以socket模式启动
样例:

[uwsgi]
# 去掉如下
# http=127.0.0.1:8000
# 改为
socket=127.0.0.1:8000

注意

http模式启动uwsgi的时候,会比socket多一个进程,因为http会多启动一个进程去解读http协议

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章