nginx安装使用
官网安装教程
1. 安装依赖
yum -y install yum-utils
2. 配置yum源
vim /etc/yum.repos.d/nginx.repo
###稳定版####
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
###最新版####
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
3. 安装nginx
yum -y install nginx
systemctl enable nginx
4. 启动停止、查看状态
systemctl start/status/stop nginx
5. 配置nginx
vim /etc/nginx/nginx.conf
6. 最佳实战配置规则
nginx官网推荐,公共配置在基础配置文件中配置,每一个server服务单独一个配置文件,然后通过include引入到基础配置中
7. 配置demo
/etc/nginx/nginx.conf
推荐把server内容考出来单独写在一个文件里,如/etc/nginx/conf.d/apiserver.conf
然后通过include引入: include /etc/nginx/conf.d/*.conf (安装好nginx默认的配置)
user root;
worker_processes 1;
#error_log logs/error.log;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
client_max_body_size 30m;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
log_format json '{"@timestamp":"$time_iso8601",'
'"@version":"1",'
'"client":"$remote_addr",'
'"url":"$uri",'
'"status":"$status",'
'"domain":"$host",'
'"host":"$server_addr",'
'"size":$body_bytes_sent,'
'"responsetime":$request_time,'
'"referer": "$http_referer",'
'"ua": "$http_user_agent"'
'}';
log_format full_json '{"@timestamp":"$time_iso8601",'
'"@version":"1",'
'"args":"$args",'
'"size":"$body_bytes_sent",'
'"content_type":"$content_type",'
'"host":"$host",'
'"host":"$server_addr",'
'"size":$body_bytes_sent,'
'"remote_addr":"$remote_addr",'
'"remote_user": "$remote_user",'
'"request_time": $request_time,'
'"server_addr": "$server_addr",'
'"status": "$status",'
'"uri": "$uri",'
'"http_referer": "$http_referer",'
'"http_x_forwarded_for": "$http_x_forwarded_for",'
'"upstream_addr": "$upstream_addr",'
'"upstream_response_time": $upstream_response_time,'
'"http_user_agent": "$http_user_agent"'
'}';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
upstream api_balance {
server 192.26.10.130:8011 weight=1 max_fails=2 fail_timeout=30s ;
server 192.26.10.132:8012 weight=1 max_fails=2 fail_timeout=30s ;
server 192.26.10.133:8013 weight=1 max_fails=2 fail_timeout=30s ;
}
server {
listen 80;
#server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
access_log logs/access_json.log full_json;
location / {
root /data/html;
index index.html index.htm;
}
location /sit {
root /data/sit/html;
index index.html index.htm;
}
location /api {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://api_balance/;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /crud {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://192.168.56.100:8002;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
#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;
#}
}
# 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;
# }
#}
}