ngxin常用

nginx

安装

wget -c https://nginx.org/download/nginx-1.11.6.tar.gz
tar -zxvf nginx-1.11.6.tar.gz
cd nginx-1.11.6
// 安装依赖
yum install gcc-c++
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
yum install -y openssl openssl-devel
//配置
./configure
//安装
make install
//检测是否成功
whereis nginx

目录

/usr/local/nginx/sbin/ 命令目录
/usr/local/nginx/conf 配置目录
nginx.conf 配置文件
cp nginx.conf nginx.conf.back 配置前先备份

基本操作

./nginx 开启 
./nginx -s stop 先查出nginx进程id再使用kill命令强制杀掉进程
./nginx -s quit 待nginx进程处理任务完毕进行停止
./nginx -s reload 重启
ps aux|grep nginx 查看进程

配置开机自启

vi /etc/rc.local
添加 /usr/local/nginx/sbin/nginx
chmod 755 /etc/rc.local

配置静态目录

1.配置alias目录方式 虚拟方式,暴露出来的并不是服务器上的真实路径
location / {
 alias  /var/www/static/;
 index  index.html index.htm;
}
2.配置root目录方式,暴露出来的是服务器上的真实路径
location / {
root /var/www/static/;
index index.html index.htm;
}
 如果是
location /abc {
 root /var/www/static/;
index index.html index.htm;
}
服务器上必须有对应的/var/www/static/abc
  • 健康检查
check interval=10000 rise=2 fall=5 timeout=1000 type=http;
check\_http\_send "HEAD / HTTP/1.0";
check\_http\_expect\_alive http\_2xx http\_3xx http\_4xx http_5xx;
  • centos查看 ngxin.config 位置
nginx -t
  • 查看安装目录
 ps -ef | grep nginx
  • 配置示例
upstream moiot_b {
     server 10.221.39.144:8011 weight=1;
	  check interval=10000 rise=2 fall=5 timeout=1000 type=http;
     check_http_send "HEAD / HTTP/1.0\r\n\r\n";
     check_http_expect_alive http_2xx http_3xx http_4xx http_5xx;
}
upstream moiot_a {
     server 10.221.39.79:8011 weight=1;
	 check interval=10000 rise=2 fall=5 timeout=1000 type=http;
     check_http_send "HEAD / HTTP/1.0\r\n\r\n";
     check_http_expect_alive http_2xx http_3xx http_4xx http_5xx;
}


server {
    listen       80;
    server_name  moiot.jd.com;
	 access_log         /xxxx main;
    error_log                /xxx warn;
    proxy_intercept_errors off;
    ignore_invalid_headers off;
    client_max_body_size 512M;
    proxy_send_timeout 600s;
    proxy_read_timeout 600s;
    gzip                    on;
    gzip_http_version       1.1;
    gzip_buffers            256 64k;
    gzip_comp_level         5;
    gzip_min_length         1000;
    gzip_types              application/x-javascript text/javascript text/css;
    location /pass/{
		        rewrite  ^/pass/(.*)$ /$1 break;
        proxy_buffering    off;
        proxy_pass  http://moiot_b;
        proxy_set_header  Host             $host:$server_port;
        proxy_set_header  X-Real-IP        $remote_addr;
        proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
    }
    location /pass_a/{
        rewrite  ^/pass_pre/(.*)$ /$1 break;
        proxy_buffering    off;
        proxy_pass  http://moiot_a;
        proxy_set_header  Host             $host:$server_port;
        proxy_set_header  X-Real-IP        $remote_addr;
        proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;

    }

}

白名单

  • 简单方法
location ^~ /test/ {
allow 10.xx.xxx.172;
deny all;
  • 直接用$http_x_forwarded_for
location 内
如果不匹配121.42.0.19 和192.168.0.0/24 则返回403
if $http_x_forwarded_for !~ (121.42.0.19|192.168.0")) {
        return 403;
        break;
}
  • 重新赋值真实ip方法,因为如果没有额外的代理服务器,真实ip是$remote_addr,http_x_forwarded_for 为空,所以此方法兼容用代理和不用代理
写在location 外
map $http_x_forwarded_for  $clientRealIp {
        ""      $remote_addr;
        ~^(?P<firstAddr>[0-9\.]+),?.*$  $firstAddr;
}
location 内
如果不匹配121.42.0.19 和192.168.0.0/24 则返回403
if ($clientRealIp !~ (121.42.0.19|192.168.0")) {
        return 403;
        break;
}
### Nginx 常用配置示例与参数说明 #### 1. server 块中的常用参数 `server` 是 Nginx 中用于定义虚拟主机的关键部分,以下是常用的 `server` 参数及其作用: - **listen**: 指定服务器监听的端口号。例如:`listen 80;` 表示监听 HTTP 的标准端口[^2]。 - **server_name**: 设置服务器的域名或 IP 地址。例如:`server_name example.com;` 可以绑定到特定的域名或 IP 地址。 #### 2. location 块中的常用参数 `location` 块用于匹配 URL 并应用相应的处理逻辑。常见的参数如下: - **root**: 指定网站的根目录路径。例如:`root /var/www/html;` 将 `/` 路径映射到该目录下的文件。 - **alias**: 类似于 `root`,但它会替换匹配的部分路径。例如,在子路由部署 React 应用时使用的 `alias` 配置[^1]: ```nginx location ^~ /anan/ { alias /usr/local/nginx/html/anan/; try_files $uri $uri/ /anan/index.html; index index.html; } ``` - **try_files**: 当访问某个 URI 不存在时尝试其他替代资源。例如:`try_files $uri $uri/ /anan/index.html;` 确保即使刷新页面也不会返回 404 错误[^1]。 - **index**: 设置默认索引文件名。例如:`index index.html index.htm;` 定义了当用户访问目录时不带具体文件名时加载哪些文件作为首页[^2]。 #### 3. 日志相关配置 Nginx 支持灵活的日志记录方式,可以通过以下指令实现: - **log_format**: 自定义日志格式。例如: ```nginx log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; ``` 这里定义了一个名为 `main` 的日志格式,包含了客户端地址、请求方法、状态码等信息[^4]。 - **access_log**: 记录访问日志的位置和格式。例如: ```nginx access_log /usr/local/etc/nginx/logs/host.access.log main; ``` 上述语句表示将访问日志保存至指定位置并采用之前定义好的 `main` 格式[^4]。 - **error_log**: 记录错误日志的位置和级别。例如: ```nginx error_log /home/log/nginx/www.anan.com.log warn; ``` #### 4. 性能优化相关配置 为了提升性能,通常还会加入一些额外选项: - **gzip**: 启用压缩传输来减少网络流量消耗。例如: ```nginx gzip on; ``` 此命令开启 Gzip 压缩功能。 #### 5. http 块的作用 `http` 块位于全局上下文中,主要用于封装多个 `server` 实体以及其他高级特性,比如代理转发、缓存管理、日志定制等功能。它还可以用来引入外部配置文件或者调整 MIME 类型支持范围等[^5]。 ```nginx http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } } ``` 以上是一个典型的最小化 Nginx 配置实例,展示了如何通过组合不同层次上的指令完成基本服务搭建工作流程。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值