老實說,從Android開發轉到後端開發,有些基礎概念仍是比較模糊的,特別是對一些框架的熟悉。其中,Nginx
算一個,因而忽然有了想搞懂Nginx
的衝動。。。php
nginx [engine x] is an HTTP and reverse proxy server, a mail proxy server, and a generic TCP/UDP proxy server, originally written by Igor Sysoev. For a long time, it has been running on many heavily loaded Russian sites including Yandex, Mail.Ru, VK, and Rambler. According to Netcraft, nginx served or proxied 26.34% busiest sites in June 2019. Here are some of the success stories: Dropbox, Netflix, Wordpress.com, FastMail.FM.html
釋義:Nginx
是一個HTTP服務器、反向代理服務器、郵件代理服務器、TCP/UDP代理服務器。java
說到這裏,就會有個疑問,和咱們一般使用的Tomcat
有何區別?nginx
Tomcat
和Nginx
的應用場景不一樣。Nginx是開源、高性能的HTTP服務器,而Tomcat更可能是一種容器,做爲Web服務器處理Java Servlet、JSP等。正則表達式
## 安裝指令
brew install nginx
## 安裝目錄
cd /usr/local/etc/nginx
複製代碼
Nginx很重要的一環就是配置文件,學習配置文件的格式以及如何使用每一個配置是基礎。apache
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
# we’re on a Solaris-based system and have determined that nginx
# will stop responding to new requests over time with the default
# connection-processing mechanism, so we switch to the second-best
use /dev/poll;
# the product of this number and the number of worker_processes
# indicates how many simultaneous connections per IP:port pair are accepted
worker_connections 2048;
}
複製代碼
http {
## include文件
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
## 文件I/O指令,該指令使用sendfile(2)直接複製數據從一個到另外一個文件描述符
sendfile on;
## 僅依賴於sendfile使用,Nginx在一個數據包中嘗試發送響應頭以及在數據包中發送一個完整的文件
#tcp_nopush on;
## 該指令指定keep-alive鏈接持續多久。
#keepalive_timeout 0;
keepalive_timeout 65;
## 開啓gzip壓縮
#gzip on;
}
複製代碼
server_name
指令邏輯分割的資源。server {
listen 8080;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
## location 指令能夠用在虛擬服務器 server 部分,井且意味着提供來自客戶端的 URI 或 者內部重定向訪問。
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
複製代碼
能夠看出,Nginx的配置是模塊化的,全局配置負責各個方面,HTTP Server、虛擬服務器則分模塊配置,每一個server_name
單獨生效。後端
一、www.nginx.cn/doc/general… 二、精通Nginx(第二版) 三、examples.javacodegeeks.com/enterprise-…tomcat