Nginx學習筆記(一)
一、為什麼學習nginx
- 絕大多數網際網路公司流量入口都選擇nginx,這是為什麼呢?究竟nginx相比於apache、tomcat有哪些優勢?有人說nginx效能高;有人說nginx多執行緒省資源;還有人說nginx程式碼寫的好?鄙人帶著探索精神想求證真相。
- 好久沒看C程式碼了,感覺有些生疏,Linux原始碼有些多,還是拿nginx這個相對的軟柿子練練手。
二、Nginx安裝
nginx官網http://nginx.org ,右側選擇download,選擇stable version進行安裝。 建議在Linux系統安裝,因為公司伺服器都是Linux(預設使用者已安裝gcc),鄙人環境Ubuntu,來來來搞起。
# 前置依賴 sudo apt-get install libpcre3 libpcre3-dev sudo apt-get install libssl-dev tar -zxvf nginx-1.14.2.tar.gz ./configure --prefix=xxxx# 安裝nginx目標目錄 make sudo make install
安裝完包含conf、html、log和sbin幾個目錄,*_temp目前我也不知道作用,猜測是和快取有關,這個問題我們先記下來,帶著問題看程式碼會更有效率。
What?原始碼安裝太繁瑣?對nginx淺嘗輒止可以參考如下,鄙人是想看原始碼,所以從原始碼安裝,
sudo apt-get install nginx
三、Nginx命令
./sbin/nginx -h nginx version: nginx/1.14.2 Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives] Options: -?,-h: this help -v: show version and exit -V: show version and configure options then exit -t: test configuration and exit -T: test configuration, dump it and exit -q: suppress non-error messages during configuration testing -s signal: send signal to a master process: stop, quit, reopen, reload -p prefix: set prefix path (default: /home/jiaxue/Software/nginx/) -c filename: set configuration file (default: conf/nginx.conf) -g directives : set global directives out of configuration file
不翻譯大家自己看,多看一手資料有好處。有不理解的歡迎線下交流,一起學習,一起進步。微信(453478062)
# 啟動 sudo ./sbin/nginx ps -ef | grep nginx# 檢查程序是否存在
不放心的使用者可以用瀏覽器再測試下,瀏覽器中輸入127.0.0.1,看到Welcome to nginx! 字樣
純伺服器使用者可以用如下方法測試:
sudo apt-get install curl curl localhost # 返回 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html> # 和nginx html目錄index.html一致
# 停止 sudo ./sbin/nginx -s quit # 或者 kill -15 程序號#-9也可以,但是不推薦
四、後續安排
- nginx配置(基本配置 + 反向代理)
- 介紹事件模型
- 學習nginx原始碼
水了一篇,文筆粗糙,各位見諒。希望廣交天下好友,一起學習一起進步。北京網際網路從業多年,當過校招社招面試官,擅長大資料、流計算、風控(反作弊)、廣告系統。