Nginx部署及常用配置方法

概述

Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,在BSD-like
协议下发行。其特点是占有内存少,并发能力强。

Nginx官网: http://nginx.org/
Ngin下载地址: http://nginx.org/download/

安装

源码安装(高扩展性)

下载服务包

wget wget http://nginx.org/download/nginx-1.22.0.tar.gz
tar zxf nginx-1.22.0.tar.gz&&mv nginx-1.22.0 nginx
mkdir /data/src&&mv nginx /data/src

安装依赖

yum -y install pcre pcre-devel zlib-devel gcc gcc-c++ make zlib

创建启动用户

useradd -M -s /sbin/nologin nginx    

编译安装

cd /data/src/nginx/
./configure --prefix=/data/nginx/ --user=nginx --group=nginx --with-http_sub_status_modele&&make&&make install

编译命令说明:
./configure
编译命令
–prefix=/data/nginx/
安装路径
–user=nginx
指定用户
–group=nginx
指定用户组
–with-http_sub_status_modele
加载模块(可加载多个空格隔开,可通过【./configure --help】查找所需的模块)

配置环境变量

echo "PATH=$PATH:/data/nginx/sbin/" >>/etc/profile
source /etc/profile

查看Nginx版本

nginx -v

查看nginx编译配置

nginx -V

检查配置文件

[root@xl02 data]# nginx -t
nginx: the configuration file /data/nginx//conf/nginx.conf syntax is ok
nginx: configuration file /data/nginx//conf/nginx.conf test is successful

启动Nginx

Nginx

Nginx状态控制

#启动服务
nginx
#重载加载
nginx -s reload
#立刻停止服务
nginx -s stop 
#完成当前进程后停止服务
nginx -s quit 
#重新打开日志文件
nginx -s reopen

yum安装(缺乏扩展性)

安装nginx

yum -y install nginx 

#配置文件 /etc/nginx/
#日志文件位置 /var/log/nginx/
#html文件位置 /usr/share/nginx/html

启动服务

systemctl start nginx

容器安装(快捷方便)

下载Nginx镜像

docker pull nginx:1.22.0

docker run部署

启动nginx容器

docker run -itd --name nginx -p 80:80  nginx:1.22.0

docker-compose部署

创建部署目录

mkdir /data/nginx/{html,ssl,log,conf.d}

创建主配文件

vim /data/nginx/nginx.conf
user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
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;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

创建server配置文件

vim /data/nginx/conf.d/default.conf
server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

    #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;

    # 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;
    #}
}

创建Nginx首页

vim /data/nginx/html/index.html
<html>
welcome nginx!
</html>

创建YML文件

vim /data/nginx/docker-compose-nginx.yml
version: '3'

services:

  nginx:
    image: nginx:1.22.0
    container_name: nginx
    restart: always
    ports:
    - 80:80
    - 443:443
    volumes:
    - /data/nginx/nginx.conf:/etc/nginx/nginx.conf
    - /data/nginx/log/:/var/log/nginx
    - /data/nginx/conf.d/:/etc/nginx/conf.d
    - /data/nginx/ssl/:/ssl
    - /data/nginx/html/:/usr/share/nginx/html
    environment:
    - TZ=Asia/Shanghai

启动容器

docker-compose -f /data/nginx/docker-compose-nginx.yml up -d

访问Nginx

[root@xl02 html]# curl 127.0.0.1
<html>
Welcome to nginx!
</html>
[root@xl02 html]# 

Nginx常用配置方法

Nginx日志优化

nginx日志配置项添加至http配置项中

http {
log_format  zdy '[$time_local]-$remote_addr-$scheme/$server_port-"$request_method"-"$uri"-($status)-$request_time s-$body_bytes_sent(bytes) $upstream_addr-($upstream_status)-$upstream_response_time s 第($connection_requests)次请
求'
                    ' body大小:$body_bytes_sent 请求长度:$request_length 发送长度:$bytes_sent';
}

server配置项中添加日志输出配置

    server {
        listen       80;
        listen       [::]:80;
        server_name  localhostname;
        root         /usr/share/nginx/html;
        charset utf-8;
        access_log /var/log/nginx/access.log zdy;
}

重启Nginx(重载服务可能不生效)

systemctl restart nginx 或 nginx -s stop && nginx

nginx前台启动

1.命令启动

nginx -g "daemon off;"

2.编辑nginx配置文件添加配置项。

daemon off;

停止nginx

nginx -s stop 

启动nginx

nginx

nginx转发TCP端口

说明:使用[–with-stream ]模块,Nginx默认不安装,可通过重新编译添加。

编辑nginx配置文件

vim /etc/nginx/nginx.conf
#将此配置添加至http{...}配置项下边
include /etc/nginx/tcp.d/*.conf;

请注意,stream配置不能放到http内,即不能放到/etc/nginx/conf.d/,因为stream是通过tcp层转发,而不是http转发。

创建配置TCP转发配置文件

mkdir -p /data/nginx/tcp.d
vim /data/nginx/tcp.d/mysql.conf
stream {
upstream mysql{
server 192.168.2.3:1521;
}
server {
listen 3306;#将192.168.2.3的1521端口转发到本机的3306端口
proxy_pass mysql;
}
}

重启nginx服务

systemctl restart nginx 或 nginx -s stop && nginx

现在就可以通过本机IP的3306/TCP访问192.168.2.3的1521/TCP端口

Nginx反向代理

添加upstream反向代理池——location转发代理池

http{

upstream test{
    server 10.172.52.53:20001;
    server2 IP:PORT;
}
    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location /test{
            proxy_pass  http://test;
        }

}

Nginx反向代理RabbitMQ管理端

http{
upstream mq {
    server 172.17.34.124:15673;
}
server{
location /mq {
            rewrite /mq/(.*)$ /$1 break;
            proxy_pass  http://mq/;
      }
}}

注意:mq访问的根为127.0.0.1:15672/还是127.0.0.1:15672/mq 此示例为:127.0.0.1:15672/mq
如果mq访问的根与location配置的不匹配也会导致无法访问或访问白屏

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值