Nginx基本使用大全

Nginx大全

本文主要記錄平時如何使用Nginx和遇到的問題

#1 環境

Mac 
CentOS6.8
Nginx
uwsgi

#2 使用

#2.1 安裝

安裝分爲兩種,一種是在Mac,另一種是CentOS

#2.1.1 在Mac下的安裝

#2.1.1.1 安裝brew

如果已經安裝,請調到下一步

  1. 安裝Command Line tools
xcode-select --install
  1. 安裝brew命令
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
#2.1.1.2 安裝Nginx
brew install nginx

20191227235313-image.png

注: Mac Homebrew 下載慢的解決方法如下:

https://blog.csdn.net/Coxhuang/article/details/103739798

#2.1.2 在CentOS下安裝

  1. 配置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
  1. 安裝nginx
yum -y install nginx

20191204235527-image.png

#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"';
    ...
}

20191228002719-image.png


#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/"

解決:

  1. 查看Nginx的進程信息

20191228003655-image.png

發現nginx進程的用戶是nginx,而我們創建的/Users/coxhuang/Documents/django_code/blog_code/script/app_sh/uwsgi/uwsgi.sock文件的用戶是root因此,只要把nginx的進程user改爲root即可

  1. 修改nginx配置

進入配置文件

// Mac
vim /usr/local/etc/nginx/nginx.conf
user  root; # 修改成root用戶,其他不變 
worker_processes  1;
...

重啓Nginx,發現又出現其他錯誤

20191228004139-image.png

我定義了root用戶,那麼nginx認爲我的組就是root 了,然而事實並不是這樣,來看root賬戶對應的組:

dscacheutil -q group | grep root

20191228005240-image.png

從查詢結果可以看到root 用戶屬於 wheel組,那我的寫法就不對了,它找不到root組,所以需要改下配置文件:

user  root wheel;
...

wheel 是一個特殊的用戶組,該組的用戶可以使用 su 切換到 root,而 staff 組是所有普通用戶的集合。

也可以將root配置到admin組裏面(二選一)

user  root admin;
...

20191228005523-image.png


發佈了112 篇原創文章 · 獲贊 36 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章