使用nginx做前端服務器
介紹
Nginx是一款輕量級高性能的Web 服務器/反向代理服務器及電子郵件(IMAP/POP3)代理服務器,其特點是佔有內存少,併發能力強
安裝
由於在安裝nginx的過程中需要g++、gcc、openssl-devel、pcre-devel和zlib-devel的依賴庫
所以先安裝所需要的依賴庫
1)安裝依賴
yum install gcc-c++
yum install pcre pcre-devel
yum install zlib zlib-devel
yum install openssl openssl--devel
2) 下載nginx
wget http://nginx.org/download/nginx-1.15.5.tar.gz
3) 解壓
解壓縮: tar -zxf nginx-1.15.5.tar.gz
產生一個nginx-1.15.5 目錄,進入nginx-1.15.5目錄
4)編譯和安裝
編譯:
./configure #默認安裝目錄是/usr/local
如果要指定安裝目錄在後面加上 --prefix=指定目錄
安裝
make
make install
5) 啓動
找到我們的 安裝目錄,如果是默認的目錄就是/usr/local/nginx,在nginx目錄下有sbin/nginx就是nginx的啓動文件,在nginx下執行
啓動:
./sbin/nginx
停止:
./sbin/nginx -s stop
重啓:
./sbin/nginx -s reload
6)給出一些編譯選項,可供選擇,一般用不到
--prefix=path 定義一個目錄,存放服務器上的文件 ,也就是nginx的安裝目錄。默認使用 /usr/local/;
--with-pcre=/pcre-8.37 設置PCRE庫的源碼路徑。PCRE庫的源碼需要從PCRE網站下載並解壓。
--with-zlib=/zlib-1.2.8 設置的zlib庫的源碼路徑。要下載從 zlib並解壓。
--with-openssl=/openssl-1.0.1t
--without-http_gzip_module 不編譯壓縮的HTTP服務器的響應模塊。編譯並運行此模塊需要zlib;
--without-http_rewrite_module 不編譯重寫模塊。編譯並運行此模塊需要PCRE庫支持;
--without-http_proxy_module 不編譯http_proxy模;
--with-http_ssl_module 使用https協議模塊。默認情況下,該模塊沒有被構建。建立並運行此模塊的OpenSSL庫是必需的;
7) 貼上三個鏈接,避免以後再找
pcre: http://ftp.pcre.org/pub/pcre http://ftp.pcre.org/pub/pcre/pcre-8.37.tar.gz
zlib: http://www.zlib.net/fossils/ http://www.zlib.net/fossils/zlib-1.2.8.tar.gz
openssl: https://ftp.openssl.org/source http://distfiles.macports.org/openssl/openssl-1.0.1k.tar.gz
8)特殊情況
我們的第一步就是爲安裝的時候提供的一個編譯環境,而我們nginx的運行是不用依賴步驟一的庫,如果有一天你有一臺無法連接外網的服務器需要你安裝nginx,而又恰好上面又沒有nginx需要的編譯環境,這個時候我們第一想法就是按上編譯環境就好了,但是由於其中的某些依賴庫在離線狀態下特別難搞定(有集成好的一體包的除外),這種情況下我們可以選擇跳過編譯過程,在另一臺可聯網設備上把編譯好的nginx包打包放到無外網的服務器上(切記nginx目錄在兩臺服務器的位置一定要一模一樣,否則會找不到目錄)
配置文件
nginx的配置文件在nginx目錄下的conf/nginx.conf
默認的配置文件
#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 {
worker_connections 1024;
}
http {
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;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
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;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
server
一個server監聽一個端口,一個nginx可以開多個端口
server {
listen 80; #監聽的端口
server_name localhost;
location / { #路徑
root html; #根目錄
index index.html index.htm; #跟文件
}
location /api {
rewrite ^/api/(.*)$ /$1 break; #重定向
proxy_pass http://127.0.0.1:8080; #代理
}
}
項目部署
nginx目錄中的html目錄是我們放靜態文件的位置,比如:我們把official_website放到html下
我們只需要修改nginx.conf的location即可
location / {
root html/official_website;
index index.html index.htm;
}
常用Linux命令
ls 遍歷當前目錄下的文件 -l 遍歷當前目錄下的文件和目錄,並顯示文件的詳細信息(不包含隱藏文件) -a 遍歷當前所有文件及目錄(包含隱藏文件) cd 進入某個目錄 mkdir 創建一個目錄 touch 創建一個文件 cat 查看一個文件的內容 rm 刪除 -r 可刪除目錄 -f 跳過確認 pwd 獲取當前目錄的路徑 scp 文件傳輸 -r 上傳多個文件或目錄 例: 文件上傳:scp file user@IP:path 文件下載:scp user@IP:filePath path tar 壓縮和解壓 解壓:tar -xf filePath.tar 壓縮: tar -cf filePath.tar filePath vi/vim 常用的文本編輯器 常用的有兩種模式:編輯模式和命令行模式,進去之後默認是命令行模式 i:進入編輯模式 esc:進入命令行模式 在命令行模式下: q:退出 w:保存 !:強制