文章目录
Nginx大全
本文主要记录平时如何使用Nginx和遇到的问题
#1 环境
Mac
CentOS6.8
Nginx
uwsgi
#2 使用
#2.1 安装
安装分为两种,一种是在Mac,另一种是CentOS
#2.1.1 在Mac下的安装
#2.1.1.1 安装brew
如果已经安装,请调到下一步
- 安装Command Line tools
xcode-select --install
- 安装brew命令
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
#2.1.1.2 安装Nginx
brew install nginx
注: Mac Homebrew 下载慢的解决方法如下:
https://blog.youkuaiyun.com/Coxhuang/article/details/103739798
#2.1.2 在CentOS下安装
- 配置yum源
vim /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
# 下面这行centos根据你自己的操作系统修改比如:OS/rehel
# 6是你Linux系统的版本,可以通过URL查看路径是否正确
baseurl=http://nginx.org/packages/centos/6/\$basearch/
gpgcheck=0
enabled=1
- 安装nginx
yum -y install nginx
#2.2 查找配置文件路径
#2.2.1 Mac
brew info nginx
Coxs-MacBook-Air:nginx coxhuang$
Coxs-MacBook-Air:nginx coxhuang$ brew info nginx
nginx: stable 1.15.12 (bottled), HEAD
HTTP(S) server and reverse proxy, and IMAP/POP3 proxy server
https://nginx.org/
/usr/local/Cellar/nginx/1.15.12 (25 files, 2MB) *
Poured from bottle on 2019-12-27 at 23:50:58
From: /Users/coxhuang/Documents/repository/homebrew-core.git/Formula/nginx.rb
==> Dependencies
Required: openssl ✔, pcre ✔
==> Options
--HEAD
Install HEAD version
==> Caveats
Docroot is: /usr/local/var/www
The default port has been set in /usr/local/etc/nginx/nginx.conf to 8080 so that
nginx can run without sudo.
nginx will load all files in /usr/local/etc/nginx/servers/.
To have launchd start nginx now and restart at login:
brew services start nginx
Or, if you don't want/need a background service you can just run:
nginx
==> Analytics
install: 36,680 (30 days), 88,942 (90 days), 406,301 (365 days)
install-on-request: 35,302 (30 days), 86,229 (90 days), 386,486 (365 days)
build-error: 0 (30 days)
输出Nginx的基本配置信息,其中/usr/local/etc/nginx/nginx.conf就是Nginx 的配置文件
#2.2.1 CentOS
CentOS下Nginx的配置文件默认在 /etc/nginx/ 目录下 nginx.conf
#2.3 配置节点文件
配置Nginx节点文件,只需要在Nginx的默认配置文件的末尾加上include就可以
include /opt/app/nginx/conf.d/*.conf; # 该路径下的所有以.conf结尾的文件都是节点文件
节点文件简单配置如下:
# Nginx + uwsgi
server {
listen 19800;
server_name localhost; # 如果服务器已经被域名解析,必须填域名
access_log /Users/coxhuang/Documents/django_code/blog_code/logs/nginx/access.log main;
error_log /Users/coxhuang/Documents/django_code/blog_code/logs/nginx/error.log;
charset utf-8;
gzip on;
gzip_types text/plain application/x-javascript text/css text/javascript application/x-httpd-php application/json text/json image/jpeg image/gif image/png application/octet-stream;
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
# 指定项目路径uwsgi
location / {
include uwsgi_params;
uwsgi_connect_timeout 30;
uwsgi_pass unix:/Users/coxhuang/Documents/django_code/blog_code/script/app_sh/uwsgi/uwsgi.sock;
}
}
#2.4 配置反向代理
# 访问80端口,就相当于于访问8000端口
server {
listen 80; # nginx开放的端口,随意设置
server_name localhost;
location / {
proxy_pass http://localhost:8000; # 反向代理到的端口
root html;
index index.html index.htm;
}
}
#2.5 配置日志文件
access_log /Users/coxhuang/Documents/django_code/blog_code/logs/nginx/access.log main;
error_log /Users/coxhuang/Documents/django_code/blog_code/logs/nginx/error.log;
注: /Users/coxhuang/Documents/django_code/blog_code/logs/nginx/ 该路径必须已经存在,如果不存在,Nginx不会创建,只会报错
#2.6 启动Nginx
#2.6.1 Mac
# 开启
sudo nginx
# 关闭
sudo nginx -s stop
# 重启
sudo nginx -s reload
#2.6.2 CentOS
# 开始
/etc/init.d/nginx start
# 关闭
/etc/init.d/nginx stop
# 重启
/etc/init.d/nginx restart
#2.7 卸载Nginx
#2.7.1 Mac
brew uninstall nginx
同时,需要将配置文件删除 /usr/local/etc/nginx/nginx.conf
#3 问题
#3.1 问题一 log_format丢失
问题描述:
nginx: [emerg] unknown log format “main” in xxx
解决 :
将log_format注释打开
http {
...
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
...
}
#3.2 问题二 权限问题
问题描述:
场景: Nginx调用uwsgi的uwsgi.sock遇到权限问题,不能操作,Nginx的error.log日志报如下错误
2019/12/28 00:16:41 [crit] 91346#0: *35 connect() to unix:/Users/coxhuang/Documents/django_code/blog_code/script/app_sh/uwsgi/uwsgi.sock failed (13: Permission denied) while connecting to upstream, client: 127.0.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", upstream: "uwsgi://unix:/Users/coxhuang/Documents/django_code/blog_code/script/app_sh/uwsgi/uwsgi.sock:", host: "127.0.0.1:19800", referrer: "http://127.0.0.1:19800/"
解决:
- 查看Nginx的进程信息
发现nginx进程的用户是nginx,而我们创建的/Users/coxhuang/Documents/django_code/blog_code/script/app_sh/uwsgi/uwsgi.sock文件的用户是root因此,只要把nginx的进程user改为root即可
- 修改nginx配置
进入配置文件
// Mac
vim /usr/local/etc/nginx/nginx.conf
user root; # 修改成root用户,其他不变
worker_processes 1;
...
重启Nginx,发现又出现其他错误
我定义了root用户,那么nginx认为我的组就是root 了,然而事实并不是这样,来看root账户对应的组:
dscacheutil -q group | grep root
从查询结果可以看到root 用户属于 wheel组,那我的写法就不对了,它找不到root组,所以需要改下配置文件:
user root wheel;
...
wheel 是一个特殊的用户组,该组的用户可以使用 su 切换到 root,而 staff 组是所有普通用户的集合。
也可以将root配置到admin组里面(二选一)
user root admin;
...