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;
}
}
复制代码