一、查看Nginx 是否安装
1、通过 nginx 进程方式
$ ps -euf | grep nginx
ps -ef 是用标准的格式显示进程
# 最经常使用的方法
$ ps aux | grep nginx
ps aux 是用 BSD 的格式来显示
如果环境部署了多个nginx, 可以通过如下方式查看当前有效Nginx 配置
# 普通管理者查阅方式
$ sudo nginx -t
# 或者(超级管理员查阅方式)
$ nginx -t
# 通过ubuntu apt-get install nginx 默认安装方式
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
2、通过 nginx 版本
$ nginx -V
# 一般输入如下内容
# nginx 配置、安装模块、日志存储目录, fastcgi, uwsgi 等
nginx version: nginx/1.14.0 (Ubuntu)
built with OpenSSL 1.1.1 11 Sep 2018
TLS SNI support enabled
configure arguments: --with-cc-opt='-g -O2 -fdebug-prefix-map=/build/nginx-GkiujU/nginx-1.14.0=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -fPIC' --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 --modules-path=/usr/lib/nginx/modules --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-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_v2_module --with-http_dav_module --with-http_slice_module --with-threads --with-http_addition_module --with-http_geoip_module=dynamic --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_xslt_module=dynamic --with-stream=dynamic --with-stream_ssl_module --with-mail=dynamic --with-mail_ssl_module
二、安装
1、通过apt-get 安装
# 更新资源, 设置国内镜像, 例如阿里云、豆瓣、华为等
$ sudo apt-get upgrade
$ sudo apt-get install nginx
2、源码安装
三、设置站点
站点设置通常放到 /etc/nginx/sites-enabled/目录下
其他 类linux 系统以具体安装默认配置文件 (nginx.conf)为准
1、静态资源反向代理
2、php, Java, python 等方向代理
http {
...
include /etc/nginx/conf.d/*.conf;
# 相关站点配置
include /etc/nginx/sites-enabled/*;
}
下面以 Flask 以及Celery 监控应用配置示例如下
pstream applicatio-alias {
# 负载均衡
#i p_hash;
# server 192.168.1.1:8000;
server 192.168.1.2:8001;
}
pstream applicatio-alias-2 {
# 负载均衡
#i p_hash;
server 192.168.1.3:8001;
}
server {
listen 443 ssl;
listen 80;
server_name application-domain;
# HTTPS协议密钥和Key
ssl_certificate /application-alias.pem;
ssl_certificate_key //application-alias.key;
ssl on;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
# 算法
ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA';
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:20m;
ssl_session_timeout 20m;
location / {
proxy_pass http://applicatio-alias;
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;
}
location /flower/ {
rewrite ^/flower/(.*)$ /$1 break;
proxy_pass http://127.0.0.1:5555;
proxy_set_header Host $host;
# proxy_pass http://127.0.0.1:5555;
# proxy_set_header Host $host;
# 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_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
# 通过域名后目录解决分发
location /api/ {
# api 接口
proxy_pass http://applicatio-alias-2;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Port $server_port;
}
location /admin {
# 静态资源路径1,例如管理端
alias /application2/dist/;
index index.html index.htm;
try_files $uri $uri/ /index.html =404;
}
location /web {
# 静态资源路径2 例如首页或者H5端
alias /application3/dist/;
index index.html index.htm;
try_files $uri $uri/ /index.html =404;
}
}
参数(变量) | 描述 |
---|---|
applicatio-alias | 应用代理名称 |
application-domain | 应用域名或者IP |
以Vue静态文件配置配置示例如下
server {
listen 80;
server_name application-domain;
root /application-path/;
index index.html index.htm;
location / {
try_files $uri $uri/ /index.html;
}
}