网站更新内容:请访问: https://bigdata.ministep.cn/

nginx的常用命令和总结

web服务器是程序员不陌生的东西,但是这一块的知识很零散,基本上是要用的时候进行查阅,除非是专业的运维哥哥才能信手拈来,所以本篇是自己在日常开发中记录的常用的知识总结

概念总结

啥是nginx

高新能HTTP和反向代理服务器,特点是占有内存少,并发能力强
支持高达50000的并发连接数
支持热部署 (也就是不重启的情况下 加载新的改动)

正向代理

客户端配置代理服务器,通过代理服务器去进行互联网访问

反向代理

客户端不需要配置代理服务器,用户直接访问代理服务器,
代理服务器去转发给要访问目标服务器,最终将目标服务器的内容返回给客户端
此时,暴露出来的是代理服务器的地址,而没有目标服务器的地址

负载均衡

增加机器数量,通过反向代理让请求分发到各个服务器,从而减少压力
分发策略:

1.轮询(默认)

也就是每个都会轮到 轮到的概率是一样的

2.weight权重

分配数值 数值越大 轮到的概率越大

upstream myServer {
proxy_pass 127.0.0.1 weight=5;
proxy_pass 127.0.0.1 weight=10;
}

3.ip_hash

每个请求按访问ip的hash结果进行分配,这样每个方可固定访问一个后端服务器
相当于这个客户端只会访问固定一台,不会轮到其他台服务器,可以解决session问题

upstream myServer {
ip_hash
proxy_pass 127.0.0.1;
proxy_pass 127.0.0.1;
}

4.fair

按照后端服务器的响应时间,那个时间短分配给哪个

upstream myServer {
proxy_pass 127.0.0.1;
proxy_pass 127.0.0.1;
fair
}

动静分离

加快网站的解析速度,把动态和静态页面放到不同服务器去解析,加快速度,降低压力


编译安装步骤(centos)

如果要安装执行版本 更改第二步骤的nginx名字就可以了 其他步骤不变

1.安装依赖

yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel

2.下载包

wget http://nginx.org/download/nginx-1.19.10.tar.gz

3.解压并到目录中

tar -zxvf nginx-1.19.10.tar.gz
cd nginx-1.19.10

4.编译安装

./configure

make &&make install


nginx的配置

组成部分

由三部分组成:
全局块 events块 http块

1.全局块:影响nginx整体运行的配置
a.worker_processes 数值越大 并发处理量越多 会受到机器限制
2.events块:影响nginx与用户连接
a.worker_connection 支持的最大连接数
3.http块:大多数功能和第三方模块的配置都在这里

又分为 http全局块 和server块

每个http块包括多个server块 一个server块相当于一个虚拟主机
每个server块也可以分为全局server块 同时包含多个location块

server块:
listen 监听端口
server_name 主机名称
location 配置路径

各部分基础写法

侦听端口

server {
# 标准的HTTP协议
listen 80;
# 标准的HTTPS协议
listen 443 ssl;
# http2
listen 443 ssl http2;
# IPv6的80端口
listen [::]:80;
# 80端口只用于IPv6
listen [::]:80 ipv6only=on;
}

访问日志

server {
# 日志文件的相对路径或完整路径
access_log /path/to/file.log;
# 开on 关off
access_log on;
}

域名

server {
# 监听一个域名
server_name yourdomain.com;
# 监听匹配的域名 例如 yourdomain.com www.yourdomain.com
server_name .yourdomain.com;
# 监听顶级域名
server_name yourdomain.
;
# 监听ip
server_name "";
}

静态资源

server {
listen 80;
server_name yourdomain.com;
location / {
root /path/to/website;
}
}

重定向

server {
listen 80;
server_name www.yourdomain.com;
return 301 http://yourdomain.com$request_uri;
}

server {
listen 80;
server_name www.yourdomain.com;
location /redirect-url {
return 301 http://otherdomain.com;
}
}

反向代理

server {
listen 80;
server_name yourdomain.com;
location / {
proxy_pass http://0.0.0.0:3000;
}
}

负载均衡

upstream node_js {
server 0.0.0.0:3000;
server 0.0.0.0:4000;
server 123.131.121.122;
}

server {
listen 80;
server_name yourdomain.com;
location / {
proxy_pass http://node_js;
}
}

https

server {
listen 443 ssl;
server_name yourdomain.com;
ssl on;
ssl_certificate /path/to/cert.pem;
ssl_certificate_key /path/to/privatekey.pem;
ssl_stapling on;
ssl_stapling_verify on;
ssl_trusted_certificate /path/to/fullchain.pem;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_session_timeout 1h;
ssl_session_cache shared:SSL:50m;
add_header Strict-Transport-Security max-age=15768000;
}

http转https

server {
listen 80;
server_name yourdomain.com;
return 301 https://$host$request_uri;
}


反向代理示例

示例1

打开浏览器 输入地址之后 直接跳转到tomcat默认页面
也就是访问80端口的页面 转发到8080端口的页面

server{
#访问80
listen 80;
server_name zzs.top;
location / {
root html;
//转发到8080
proxy_pass http://127.0.0.1:8080;
index index.html index.htm;
}
}

示例2

server{
listen 9001;
server_name 192.168.17.129;
location ~ /edu/ {
proxy_pass http://127.0.0.1:80;
}
location ~ /vod/ {
proxy_pass http://127.0.0.1:8081;
}
}

负载均衡示例

http {
//负载组名字 myServer
upstream myServer {
//服务器列表
server 192.168.17.129:8080;
server 192.168.17.129:8081;
}

server {
listen 80;
server_name 192.168.17.129;
location / {
proxy_pass http://myServer;
root html;
index index.html index.htm;
}
}
}

动静分离示例

不是所谓的静态跟动态进行物理分离,
而是将请求动态和请求静态的分离开
可以理解成 客户端请求的时候
如果请求是动态那么就通过nginx转发到tomcat服务器
如果是请求静态那么就通过nginx转发到一个静态资源服务器

server{
#动静分离配置
listen 80;
server_name 192.168.231.128;
#访问网页
location /www/ {
root /data/;
index index.html index.htm;
}
#访问资源
location /image/ {
root /data/;
#列出访问目录
autoindex on;
}
}

高可用的集群

给nginx做主从复制一样
比如两台服务器来实现负载均衡
当master出现问题的时候 因为做了主从复制
改成请求backup 不需要再此去配置
需要使用软件 keepalived

基础配置

1.比如有两台服务器 192.168.17.129(主) 和 192.168.17.131(从)
2.两台服务器分别安装 nginx keepalived

keepalived:

1.yum install -y keepalived

2.keepalived.conf 为主要配置文件

修改keepalived.conf的配置文件 配置主从 和 增加虚拟ip

在/usr/local/src下添加 检测脚本 nginx_check.sh

!/bin/bash

A=ps -C nginx -no-header |wc -l
if [$A -eq 0];then
/usr/local/nginx/sbin/nginx
sleep 2
if [ps -C nginx --no-header |ws -l -eq 0];then
killall keepalived
fi
fi

两台都进行以上配置 之后 开启两台的nginx 并启动keepalived
nginx -s start (该命令随安装的时候有一样的执行方式)
systemctl start keepalived.service

4.访问虚拟ip进行测试 之后将主的nginx 和keepalived停掉
看是否能访问到从

配置详解
第一部分:全局配置

golbal_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.17.129(主)
smtp_connect_timeout 30
router_id LVS_DEVELBACK #访问到主机 服务器的名字 在hosts文件下配置
}

第二部分

vrrp_script chk_http_post {
#这里的路径就是检测脚本的路径
script "/usr/local/src/nginx_check.sh"
interval 2 #(检测脚本执行的间隔)
weight 2
}

第三部分

vrrp_instance VI_1 {
state BACKUP #备份服务器上 将MASTER 改成 BACKUP
interface ens33 #网卡
virtual_router_id 51 #主 备机的virtual_router_id 必须相同
priority 90 #主备机取不同的优先级,主机值较大,备份机较小
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.17.50 //VRRP H虚拟地址
}

}


常用反向代理配置全码

user nobody;

worker_processes 2;

error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

pid logs/nginx.pid;

events {
worker_connections 8096;
multi_accept on;
use epoll;
}
worker_rlimit_nofile 40000;

http {
include mime.types;
default_type application/octet-stream;

log_format main '"$remote_addr $http_x_forwarded_for_$remote_user [$time_local]"'
'"$request"$status $body_bytes_sent"'
'"$http_referer" "$http_user_agent"'

access_log logs/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 15;

gzip on;
client_max_body_size 30m;

server {

listen 73;
server_name localhost;

location / {
proxy_redirect off;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://101.32.73.3:9001/;
proxy_buffering off;
proxy_buffer_size 128k;
proxy_buffers 100 128k;
}
#access_log logs/access.log main;
}
}

Yii框架配置nginx

server
{
listen 74;
server_name localhost;
index index.php index.html index.htm default.php default.htm default.html;
root /Users/javashishijieshangzuihaodeyuyan/project/PHP/news/frontend/web;

SSL-START SSL相关配置,请勿删除或修改下一行带注释的404规则

error_page 404/404.html;

SSL-END

ERROR-PAGE-START 错误页配置,可以注释、删除或修改

error_page 404 /404.html;

error_page 502 /502.html;

ERROR-PAGE-END

PHP-INFO-START PHP引用配置,可以注释或修改

location ~ .php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

PHP-INFO-END

REWRITE-START URL重写规则引用,修改后将导致面板设置的伪静态规则失效

location / {
if (!-e $request_filename){
rewrite ^(.*)$ /index.php?s=$1 last; break;
}
}
#REWRITE-END

禁止访问的文件或目录

location ~ ^/(.user.ini|.htaccess|.git|.svn|.project|LICENSE|README.md)
{
return 404;
}

一键申请SSL证书验证目录相关设置

location ~ .well-known{
allow all;
}

location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
error_log /dev/null;
access_log off;
}

location ~ .*.(js|css)?$
{
expires 12h;
error_log /dev/null;
access_log off;
}
}

WS反向代理

server {

listen 83;
server_name aaaa.com;
location / {

proxy_redirect off;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://11.3.7.5:9002/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}

lumen框架配置

server
{
listen 80;
#listen [::]:80;
server_name zhangzeshan.top ;
index index.html index.htm index.php default.html default.htm default.php;
root /home/wwwroot/default/api/public;

include rewrite/none.conf;
#error_page 404 /404.html;

Deny access to PHP files in specific directory

location ~ /(wp-content|uploads|wp-includes|images)/.*.php$

include enable-php.conf;

location ~ ..(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ .
.(js|css)?$
{
expires 12h;
}

location ~ /.well-known {
allow all;
}

location ~ /.
{
deny all;
}

access_log off;
}

ThinkPHP配置

server {

listen 80;

本地虚拟域名

server_name www.test.com ;

项目存放路径

root "D:\phpProject\pay";

location / {

index index.html index.htm index.php;

autoindex on;

if (!-e $request_filename){

rewrite ^(.*)$ /index.php?s=$1 last;

break;

} }

location ~ .php {

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

fastcgi_split_path_info ^((?U).+.php)(/?.+)$;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

fastcgi_param PATH_INFO $fastcgi_path_info;

fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;

include fastcgi_params;

}}
(27条消息) nginx的常用命令和总结_zhangzeshan-CSDN博客

posted @ 2022-03-05 09:18  ministep88  阅读(284)  评论(0)    收藏  举报
网站更新内容:请访问:https://bigdata.ministep.cn/