Nginx概述
- 网站服务器、反向代理器、负载均衡器
- 异步事件处理模型(Nginx),更高并发,同时支持5万并发连接。多线程客户机处理模型(Apache)
- 模块化设计、丰富模块库
安装
root@iZ8vbgll9tzu0dgfcvf6s4Z:~# sudo apt-get install nginx
root@iZ8vbgll9tzu0dgfcvf6s4Z:~# nginx -v
nginx version: nginx/1.14.0 (Ubuntu)
root@iZ8vbgll9tzu0dgfcvf6s4Z:~#
常用命令
# 启动
root@iZ8vbgll9tzu0dgfcvf6s4Z:~# sudo nginx
# 停止,快速kill进程
root@iZ8vbgll9tzu0dgfcvf6s4Z:~# sudo nginx -s stop
# 停止Nginx
root@iZ8vbgll9tzu0dgfcvf6s4Z:~# sudo nginx -s quit
# 重新加载配置文件
root@iZ8vbgll9tzu0dgfcvf6s4Z:~# sudo nginx -s reload
# 重新打开日志文件
root@iZ8vbgll9tzu0dgfcvf6s4Z:~# sudo nginx -s reopen
浏览器访问对应IP
默认配置目录
root@iZ8vbgll9tzu0dgfcvf6s4Z:~# cd /etc/nginx/
root@iZ8vbgll9tzu0dgfcvf6s4Z:/etc/nginx#
root@iZ8vbgll9tzu0dgfcvf6s4Z:/etc/nginx# ll
total 72
drwxr-xr-x 8 root root 4096 Mar 29 00:25 ./
drwxr-xr-x 89 root root 4096 Mar 29 00:25 ../
drwxr-xr-x 2 root root 4096 Jan 11 2020 conf.d/
-rw-r--r-- 1 root root 1077 Apr 6 2018 fastcgi.conf
-rw-r--r-- 1 root root 1007 Apr 6 2018 fastcgi_params
-rw-r--r-- 1 root root 2837 Apr 6 2018 koi-utf
-rw-r--r-- 1 root root 2223 Apr 6 2018 koi-win
-rw-r--r-- 1 root root 3957 Apr 6 2018 mime.types
drwxr-xr-x 2 root root 4096 Jan 11 2020 modules-available/
drwxr-xr-x 2 root root 4096 Mar 29 00:25 modules-enabled/
-rw-r--r-- 1 root root 1482 Apr 6 2018 nginx.conf
-rw-r--r-- 1 root root 180 Apr 6 2018 proxy_params
-rw-r--r-- 1 root root 636 Apr 6 2018 scgi_params
drwxr-xr-x 2 root root 4096 Mar 29 00:25 sites-available/
drwxr-xr-x 2 root root 4096 Mar 29 00:25 sites-enabled/
drwxr-xr-x 2 root root 4096 Mar 29 00:25 snippets/
-rw-r--r-- 1 root root 664 Apr 6 2018 uwsgi_params
-rw-r--r-- 1 root root 3071 Apr 6 2018 win-utf
root@iZ8vbgll9tzu0dgfcvf6s4Z:/etc/nginx#
root@iZ8vbgll9tzu0dgfcvf6s4Z:/etc/nginx#
- nginx.conf 默认配置文件
- site-enabled 启用的配置文件夹
- sites-available 可使用的配置文件
关键配置模块
- events配置域:网络连接相关配置(I/O模型等)
- server配置域:相关服务节点配置
- location配置域:资源路由配置
- http配置域:Nginx作为网页服务器时的配置(#mail)
- upstream配置域:反向代理配置域
关键配置模块层级关系
events {
...
}
http {
...
upstream {
...
}
server {
...
location {
...
}
}
}
mail {
...
}
nginx.conf
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
#mail {
# # See sample authentication script at:
# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
# # auth_http localhost/auth.php;
# # pop3_capabilities "TOP" "USER";
# # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
# server {
# listen localhost:110;
# protocol pop3;
# proxy on;
# }
#
# server {
# listen localhost:143;
# protocol imap;
# proxy on;
# }
#}
为什么需要Nginx进行部署
- 知名服务器,Nginx提供更加安全的服务
- 提供反向代理,负载均衡等丰富服务
- 对于静态文件的处理能力更强
root@iZ8vbgll9tzu0dgfcvf6s4Z:~# cd /etc/nginx/sites-available
root@iZ8vbgll9tzu0dgfcvf6s4Z:~# cp nginx.cnf nginx-uwsgi.conf
nginx-uwsgi.conf
..
http {
...
# 反向代理
upstream uwsgi {
server 127.0.0.1:8000;
}
server {
lister 80;
server_name .**.com
charset utf-8;
access_log /var/log/nginx/nginx-uwsgi.log;
location / {
proxy_pass http://uwsgi;
}
location /static {
alias /**/django_deployment/static;
}
}
}
root@iZ8vbgll9tzu0dgfcvf6s4Z:~# ln -s /etc/nginx/sites-available/nginx-uwsgi.conf /etc/nginx/nginx.conf
uwsgi_pass和proxy_pass
- uwsgi_pass使用uwsgi协议,uwsgi是uWSGI服务器的主要协议
- proxy_pass使用普通的HTTP与uWSGI服务器联系