yum -y install gcc gcc-c++ autoconf pcre-devel make automake
yum -y install wget httpd-tools vim
複製代碼
vim /etc/yum.repos.d/nginx.repophp
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
複製代碼
yum install nginx
nginx -v
複製代碼
/etc/nginx/nginx.confhtml
#運行用戶,默認便是nginx,能夠不進行設置
user nginx;
#Nginx進程,通常設置爲和CPU核數同樣
worker_processes 1;
#錯誤日誌存放目錄
error_log /var/log/nginx/error.log warn;
#進程pid存放位置
pid /var/run/nginx.pid;
events {
worker_connections 1024; # 單個後臺進程的最大併發數
}
http {
include /etc/nginx/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 /var/log/nginx/access.log main; #nginx訪問日誌存放位置
sendfile on; #開啓高效傳輸模式
#tcp_nopush on; #減小網絡報文段的數量
keepalive_timeout 65; #保持鏈接的時間,也叫超時時間
#gzip on; #開啓gzip壓縮
include /etc/nginx/conf.d/*.conf; #包含的子配置項位置和文件
複製代碼
/etc/conf.d/default.conflinux
server {
listen 80; #配置監聽端口
server_name localhost; //配置域名
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html; #服務默認啓動目錄
index index.html index.htm; #默認訪問文件
}
#error_page 404 /404.html; # 配置404頁面
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html; #錯誤狀態碼的顯示頁面,配置後須要重啓
location = /50x.html {
root /usr/share/nginx/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;
#}
}
複製代碼
方式1:
nginx
方式2:
systemctl start nginx.service
複製代碼
(1) 從容中止服務
nginx -s quit
(2) 當即中止服務
nginx -s stop
(3) 殺死進程
killall nginx
(4) 服務命令中止
systemctl stop nginx.service
複製代碼
(1) 從新載入配置文件
nginx -s reload
(2) 服務方式中止
systemctl restart nginx.service
複製代碼
server{
listen 8001;
server_name localhost;
root /usr/share/nginx/html/html8001;
index index.html;
}
複製代碼
server{
listen 80;
server_name 112.74.164.244;
root /usr/share/nginx/html/html8001;
index index.html;
}
複製代碼
下面代碼意思是監聽80端口,當用戶訪問nginx2.jspang.com時,將用戶訪問代理到http://jspang.com域名下.nginx
server{
listen 80;
server_name nginx2.jspang.com;
location / {
proxy_pass http://jspang.com;
}
}
複製代碼
其餘反向代理命令c++
proxy_set_header :在將客戶端請求發送給後端服務器以前,更改來自客戶端的請求頭信息。web
proxy_connect_timeout:配置Nginx與後端代理服務器嘗試創建鏈接的超時時間。vim
proxy_read_timeout : 配置Nginx向後端服務器組發出read請求後,等待相應的超時時間。後端
proxy_send_timeout:配置Nginx向後端服務器組發出write請求後,等待相應的超時時間。centos
proxy_redirect :用於修改後端服務器返回的響應頭中的Location和Refresh。bash
server{
listen 80;
server_name nginx2.jspang.com;
location / {
root /usr/share/nginx/pc;
if ($http_user_agent ~* '(Android|webOS|iPhone|iPod|BlackBerry)') {
root /usr/share/nginx/mobile;
}
index index.html;
}
}
複製代碼