Nginx 是一個高性能的HTTP和反向代理服務(wù)器,也是一個IMAP/POP3/SMTP服務(wù)器。
安裝nginx:
sudo apt-get install nginx
啟動nginx(一般都是自動啟動的):
格式:nginx安裝目錄地址 -c nginx配置文件地址
如: /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx啟動后,可以通過以下命令進行控制:
nginx -s signal
signal可以是:
- stop — fast shutdown
- quit — graceful shutdown
- reload — reloading the configuration file
- reopen — reopening the log files
其中,quit會等到工作進程服務(wù)完現(xiàn)有的請求后才執(zhí)行:
$ nginx -s quit
查看配置文件:
$ cat /etc/nginx/nginx.conf
nginx.conf 可能在其他路徑下,如/usr/local/nginx/conf 或 /usr/local/etc/nginx.
#nginx.conf
user www-data;
worker_processes auto;
pid /run/nginx.pid;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
gzip on;
gzip_disable "msie6";
application/xml application/xml+rss text/javascript;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
從nginx.conf可以看出配置文件的結(jié)構(gòu):events、http在main環(huán)境中,server在http中,location在server中。其中,http中,有兩個include:
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
指向/etc/nginx/conf.d/和/etc/nginx/sites-enabled/下的配置文件,其中/etc/nginx/sites-enabled/下的default鏈接指向/etc/nginx/sites-available/default。
$ cd /etc/nginx/sites-enabled/
$ ls -l
輸出:
lrwxrwxrwx 1 root root 34 Sep 12 13:42 default -> /etc/nginx/sites-available/default
例如要配置一個代理服務(wù)器,可以直接在/etc/nginx/sites-available/default寫進一個server。
首先,先備份default文件:
$ cd /etc/nginx/sites-available/
$ sudo cp default default-backup
在default文件中寫入server:
server {
location / {
proxy_pass http://localhost:8080/;
}
location ~ \.(gif|jpg|png)$ {
root /data/images;
}
}
這個server會過濾的請求,所有以 .gif, .jpg, .png 結(jié)尾的請求,例如http://localhost/example.png,nginx將會把文件/data/images/example.png響應,如果沒有這個文件則返回404error;其他的請求則全部轉(zhuǎn)向代理服務(wù)器http://localhost:8080/。
修改完配置,重載一下:
$ nginx -s reload