- 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協議