我備案了個域名,買了一個阿里雲服務器,想要搭建幾個本身的網站,不免要接觸 nginx。html
那麼我用 nginx 來幹嗎呢:nginx
yum install -y nginx
到官網下載,安裝包,解壓便可使用:官網windows
# 開機啓動 systemctl enable nginx.service # 啓動 systemctl start nginx.service # 使用某個配置文件啓動(要先關閉 ngxin,否則會報錯的) nginx -c /etc/nginx/nginx.conf # 關閉(若是這樣關閉不了的話,就把 nginx 進程給殺掉) nginx -s stop # 查看 nginx 的進程 id ps -ef | grep nginx # 殺死進程(好比殺死進程1234) kill -9 1234
nginx 的默認配置文件是:/etc/nginx/nginx.conf
centos
這個配置文件會自動讀取:/etc/nginx/conf.d/
文件夾下的全部 .conf
文件。服務器
假如你寫了個網站,須要用到 nginx ,那麼你就把配置文件丟到 /etc/nginx/conf.d/
文件夾下,而後重啓 nginx 就好了:網站
nginx -s stop nginx -c /etc/nginx/nginx.conf
假設咱們有兩個網站:www.example.com
blog.example.com
阿里雲
它們分別有兩個 ngxin 配置文件放在:/home/www.example.com/www.example.com.nginx.conf
/home/blog.example.com/blog.example.com.nginx.conf
url
服務器對外開放 80 端口,兩個網站對應的程序端口分別爲:centos7
www.example.com 程序 8000 www.example.com 靜態資源 8080 blog.example.com 程序 8001 blog.example.com 靜態資源 8081
那麼他們的配置內容分別爲:代理
# /home/www.example.com/www.example.com.nginx.conf server { # 服務器對外只監聽 80 端口 listen 80; # 當 80 端口監聽到了來自 www.example.com 的請求 server_name www.example.com; # 那麼把這個請求轉到 http://localhost:8080 location / { proxy_pass http://localhost:8080; } } server { # 監聽到 8080 端口有人發起請求(其實就是上面轉過來的↑,用戶不能直接訪問 8080 端口的) listen 8080; # 只容許本機訪問 server_name localhost; # 程序根目錄 root /home/www.example.com; # 默認的網頁文件 index index.html index.htm; # 匹配全部 uri (若是 url 爲:http://www.example.com/hehe,那麼 uri 爲:/hehe) location / { # 咱們的程序其實是在 8000 端口上 proxy_pass http://localhost:8000$request_uri; # 下面這一大堆,欲知詳情本身查 proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504; proxy_max_temp_file_size 0; proxy_connect_timeout 90; proxy_send_timeout 90; proxy_read_timeout 90; proxy_buffer_size 4k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k; } # 匹配首頁 location = / { proxy_pass http://localhost:8000; } # 匹配 /**.** 結尾的 uri,而且不是 /**.html、/**.htm location ~* ^.+\/[^\/]+(?=\.)([^\/](?!(html|htm)))+$ { # 匹配到的都定位到靜態資源目錄裏 root /home/www.example.com/static; etag on; expires max; } }
# /home/blog.example.com/blog.example.com.nginx.conf server { # 服務器對外只監聽 80 端口 listen 80; # 當 80 端口監聽到了來自 blog.example.com 的請求 server_name blog.example.com; # 那麼把這個請求轉到 http://localhost:8081 location / { proxy_pass http://localhost:8081; } } server { # 監聽到 8081 端口有人發起請求(其實就是上面轉過來的↑,用戶不能直接訪問 8081 端口的) listen 8081; # 只容許本機訪問 server_name localhost; # 程序根目錄 root /home/blog.example.com; # 默認的網頁文件 index index.html index.htm; # 匹配全部 uri (若是 url 爲:http://blog.example.com/hehe,那麼 uri 爲:/hehe) location / { # 咱們的程序其實是在 8001 端口上 proxy_pass http://localhost:8001$request_uri; # 下面這一大堆,欲知詳情本身查 proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504; proxy_max_temp_file_size 0; proxy_connect_timeout 90; proxy_send_timeout 90; proxy_read_timeout 90; proxy_buffer_size 4k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k; } # 匹配首頁 location = / { proxy_pass http://localhost:8001; } # 匹配 /**.** 結尾的 uri,而且不是 /**.html、/**.htm location ~* ^.+\/[^\/]+(?=\.)([^\/](?!(html|htm)))+$ { # 匹配到的都定位到靜態資源目錄裏 root /home/blog.example.com/static; etag on; expires max; } }
注意看兩個配置的區別。
假如咱們每一個網站程序放在一個文件夾裏,該程序的 nginx 配置文件也應該放在這個文件夾裏才方便管理。但前面提到,咱們須要把配置文件丟到 /etc/nginx/conf.d/
文件夾下,怎樣才能使這個配置文件既在程序文件夾下,又在 /etc/nginx/conf.d/
文件夾下呢?
假如咱們在程序文件夾下有一個 ngxin 配置文件:/home/www.example.com/www.example.com.nginx.conf
咱們須要給這個文件建立一個軟連接到 /etc/nginx/conf.d/
下:
ln -s /home/example/www.example.com.nginx.conf /etc/nginx/conf.d/www.example.com.nginx.conf
這樣操做以後,當咱們改程序文件夾下的配置文件,/etc/nginx/conf.d/
下與之對應的配置文件也會被修改,修改後重啓 nginx 就可以使新的 ngxin 配置生效了。