介紹
使用Nginx不難,但是如果你剛開(kāi)始接觸,需要花點(diǎn)時(shí)間才能理解Nginx到底是個(gè)什么意思,而相應(yīng)的反向代理又是什么意思?
知乎上看到一個(gè)圖,簡(jiǎn)單明了
Paste_Image.png
使用方法(ubuntu環(huán)境)
安裝
$ apt-get install nginx-
主要配置文件
安裝后 目錄在/etc/nginx
一般只需修改/etc/nginx/sites-available/default文件 就行了
日志文件在 /var/log/nginx 配置及使用
不使用nginx的時(shí)候 假設(shè)有兩個(gè)程序,地址分別為
http://127.0.0.1:8080
http://127.0.0.1:8082
server {
listen 80;
server_name a.com;
charset utf-8;
access_log /home/a.com.access.log;
location /(css|js|img)/ {
access_log off;
expires 1d;
root “/home/rick/static";
try_files $uri @backend;
}
location / {
try_files /_not_exists_ @backend;
}
location @backend {
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://127.0.0.1:8080;
}
}
server {
listen 80;
server_name b.com;
charset utf-8;
access_log /home/a.com.access.log;
location /(css|js|img)/ {
access_log off;
expires 1d;
root “/home/rick/static";
try_files $uri @backend;
}
location / {
try_files /_not_exists_ @backend;
}
location @backend {
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://127.0.0.1:8082;
}
}
- 啟動(dòng)
$ service nginx start //reload restart stop
設(shè)置完后,訪問(wèn)a.com 和b.com 就分別訪問(wèn)了8080 和8081