CentOS搭建Nginx服务

简书: https://www.jianshu.com/p/5c3938ce2cf6

1.官网下载

https://nginx.org/en/download.html

 

 

2.上传CentOS服务

3.解压

1
tar zxf nginx-1.21.6.tar.gz

4.configure:生成Makefile

1
cd nginx-1.21.6

  编译并指定安装位置

1
./cofigure --prefix=/usr/local/nginx

  错误解决

1
2
错误:"./configure: error: C compiler cc is not found"
解决:yum -y install gcc-c++

  编译成功

1
2
3
4
5
6
7
8
9
10
11
12
13
nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx modules path: "/usr/local/nginx/modules"
nginx configuration prefix: "/usr/local/nginx/conf"
nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/nginx/logs/error.log"
nginx http access log file: "/usr/local/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp

5.编译

1
make

6.安装

1
make install

  

 了解: configure, make, make install

https://www.cnblogs.com/python99/p/12242613.html

复制代码
configure
这一步一般用来生成Makefile,为下一步的编译做准备,你可以通过在configure后加上参数来对安装进行控制,比如代码:./configure --prefix=/usr上面的意思是将谇软件安装在/usr下面,执行文件就会安装在/usr/bin,同时一些软件的配置文件你可以通过指定--sys-config=参数进行设定。有一些软件还可以加上--with, --enable, --without, --disable等参数对编译加以控制,你可以通过允许./configure --help查看详细的说明帮助。

make
这一步就是编译,大多数的源代码包都经过这一步进行编译,如果在make过程中出现error,可以向开发者提交bugreport(一般在install里有提交地址),或者你的系统少了一些依赖库等。make的作用是开始进行源代码编译,以及一些功能的提供,这些功能由它的Makefile设置文件提供相磁的功能。

make是Linux开发套件里面自动化编译的一个控制程序,他通过借助Makefile里面编写的编译规范进行自动化调用gcc、ld以及某些需要的程序进行编译的程序。

make install
进行安装(当然有些软件需要先运行make check或make test来进行一些测试)。如果原始代码编译无误,且执行结果正确,便可以把程序安装至系统预设的可执行文件存放路径。如果用bin_PROGRAMS宏的话,程序会被安装至/usr/local/bin这个目录,或者库文件拷贝到相应的目录下

make clean可以清除编译产生的可执行文件及目标文件。
View Code
复制代码

 

7.启动

1
2
3
cd /usr/local/nginx
cd sbin
./nginx

8.开启、关闭防火墙

1
2
systemctl start firewalld
systemctl stop firewalld

   # 防火墙放行端口
  firewall-cmd --zone=public --add-port=80/tcp --permanent


   # 重启防火墙
  firewalld-cmd --reload

 

 

linux关闭防火墙与开启防火墙命令 

https://www.cnblogs.com/goOJBK/p/15703960.html

 

9. 启动和关闭脚本 [nginx.service]

vi /usr/lib/systemd/system/nginx.service

nginx.service

复制代码
[Unit]
Description=nginx - web server
After=network.target remote-fs.target nss-lookup.target 

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf 
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf 
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecQuit=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target
复制代码

 

  脚本操作

1.杀死进程

kill -quit  <master pid>

2.重新加载nginx服务

1
systemctl daemon-reload nginx.service

3.启动,关闭,状态,进程

1
2
3
4
5
6
systemctl start nginx.service
systemctl stop nginx.service
systemctl status nginx.service
 
systemctl status nginx
ps -ef | grep nginx

  

 其他

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
一台主机开一个nginx服务,监听多个代理服务
同一个nginx.conf配置多个server:
server {
  listen 80;
  server_name xx.com;
  location / {
    proxy_pass http://xx.com:8081/;
  }
}<br><br>server {
  listen 80;
  server_name yy.com;
  location / {
    proxy_pass http://yy.com:8082/;
  }
}
 
一台主机开多个nginx服务,监听同一个代理服务
 
 
负载均衡::
nginx.conf
upstream xx {
    server 111.22.33.55:88;
    server 111.22.33.55:99;
}
 
server {
  listen 80;
  server_name localhost;
  location / {
    proxy_pass http://xx;
  }
}
 
权重:有的机器性能好,接收请求分多点;有的机器性能差,接收请求分少点
nginx.conf
upstream xx {
    server 111.22.33.55:88 weight=1;
    server 111.22.33.55:99 weight=5;
}
 
tomcat和nginx动静分离
将静态资源复制到nginx,并配置nginx.conf
 
url重写 : rewrite语法
rewrite ^/list-([0-9]+).html$ /list?id=$1 last;
http://localhost:8080/list?id=1
nginx替换为
http://localhost:8080/list/1
场景:短网址

server {
  listen 30763;
  server_name 192.168.1.20;
  location / {
    proxy_pass http://192.168.1.20:30763/api-web/;
  }
}

  ==========================================================================2023-10-30=====================================================

web应用服务器

Apache, Nginx

排名

https://w3techs.com/technologies/overview/web_server

Nginx优势:

  1. 高性能:处理高并发能力强。 ( I/O模型:epoll模型。)
  2. 高扩展性:模块化程序,根据需求添加相应模块。
  3. 高可靠性:高可用机制。达到运维 "三个9,四个9,五个9" 的高可靠性。
  4. 支持热部署:平滑升级,不影响用户访问。

Nginx应用场景:

  1. web服务:实现用户访问请求处理。
  2. 负载均衡:实现集群化管理。
  3. 代理缓存:将动态请求的资源在本地进行预缓存,提升用户的访问速度。
  4. 安全服务:针对用户访问进行权限控制,https加密处理的安全认证,结合Lua语言实现waf防护(网页防火墙) , 防止cc攻击,防止sql注入。

Nginx安装

工具 版本
Nginx 1.18

 

 1.Nginx三种安装方式:

  1. 编译安装:私人定制,根据需求,需要什么功能,编译时添加什么模块。
  2. 自动化安装:配置好安装源(种子),直接安装。
  3. 二进制安装:类似于绿色免安装版本,编译安装好的程序封装起来的包。

 2.Nginx编译安装

   准备源码包,解压

   预编译 ---> 编译 ---> 安装

    准备依赖环境

yum install -y openssl-devel pcre-devel

     预编译

1
2
3
4
5
6
7
./configure --prefix=/usr/local/nginx \
  --sbin-path=/bin/   \
  --user=nginx   \
  --group=nginx \
  --with-http_ssl_module \
  --with-http_stub_status_module \
  --with-http_gzip_static_module

  安装

make && make install

       检查当前版本

/bin/nginx -V

       添加nginx用户

useradd nginx  -m -s /sbin/nologin
id nginx #查看用户

       启动

/bin/nginx
netstat -luntp | grep nginx

 3.Nginx自动化安装

官方安装文档:https://nginx.org/en/linux_packages.html

 Nginx目录结构

复制代码

[root@prome-master01 ~]# rpm -ql nginx
/etc/logrotate.d/nginx           # 日志切割  (默认以天为单位)
/etc/nginx/fastcgi.conf          
/etc/nginx/fastcgi.conf.default
/etc/nginx/fastcgi_params
/etc/nginx/fastcgi_params.default
/etc/nginx/koi-utf
/etc/nginx/koi-win
/etc/nginx/mime.types                  #Nginx存放响应报文中回传的文件类型
/etc/nginx/mime.types.default
/etc/nginx/nginx.conf                  #Nginx主配置文件            
/etc/nginx/nginx.conf.default          #Nginx默认配置文件
/etc/nginx/scgi_params
/etc/nginx/scgi_params.default
/etc/nginx/uwsgi_params
/etc/nginx/uwsgi_params.default
/etc/nginx/win-utf


/usr/bin/nginx-upgrade
/usr/lib/systemd/system/nginx.service
/usr/lib64/nginx/modules
/usr/sbin/nginx                             #Nginx主程序存放路径
/usr/share/doc/nginx-1.20.1
/usr/share/doc/nginx-1.20.1/CHANGES
/usr/share/doc/nginx-1.20.1/README
/usr/share/doc/nginx-1.20.1/README.dynamic
/usr/share/doc/nginx-1.20.1/UPGRADE-NOTES-1.6-to-1.10
/usr/share/licenses/nginx-1.20.1
/usr/share/licenses/nginx-1.20.1/LICENSE
/usr/share/man/man3/nginx.3pm.gz
/usr/share/man/man8/nginx-upgrade.8.gz
/usr/share/man/man8/nginx.8.gz

/usr/share/nginx/html/                  #Nginx默认存放网站源码的位置
/usr/share/nginx/html/404.html
/usr/share/nginx/html/50x.html
/usr/share/nginx/html/en-US
/usr/share/nginx/html/icons
/usr/share/nginx/html/icons/poweredby.png
/usr/share/nginx/html/img
/usr/share/nginx/html/index.html
/usr/share/nginx/html/nginx-logo.png
/usr/share/nginx/html/poweredby.png
/usr/share/nginx/modules
/usr/share/vim/vimfiles/ftdetect/nginx.vim
/usr/share/vim/vimfiles/ftplugin/nginx.vim
/usr/share/vim/vimfiles/indent/nginx.vim
/usr/share/vim/vimfiles/syntax/nginx.vim


/var/lib/nginx
/var/lib/nginx/tmp
/var/log/nginx
/var/log/nginx/access.log
/var/log/nginx/error.log

复制代码

 Nginx主配置文件

复制代码
# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

#指定Nginx服务运行的用户
user nginx;       
#指定Nginx的worker进程数量
worker_processes auto;
#指定Nginx错误日志
error_log /var/log/nginx/error.log;
#指定Nginx的PID进程号文件
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

#指定Nginx当前一个worker进程同时处理的最大请求连接数量
events {
    worker_connections 1024;  
}

#
http {
    #指定Nginx访问日志格式
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    #指定Nginx访问日志位置
    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    #Nginx建立TCP连接后,多久没有动作后自动断开
    keepalive_timeout   65;
    types_hash_max_size 4096;

    include             /etc/nginx/mime.types;
    #当Nginx无法识别当前访问页面的内容时,触发下载动作
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    #指定自配置路径下所有.conf结尾的文件
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80;
        listen       [::]:80;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        error_page 404 /404.html;
        location = /404.html {
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
    }

# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2;
#        listen       [::]:443 ssl http2;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers HIGH:!aNULL:!MD5;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

}
复制代码

Nginx子配置文件(站点配置文件)

复制代码
#Nginx子配置 (站点配置)
    server {
        #指定Nginx监听的端口
        listen       80;
        listen       [::]:80;
        #指定当前网站的访问域名
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        error_page 404 /404.html;
        location = /404.html {
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
        
        location / {
            #指定代码位置
            root /html/game;
            #指定首页位置
            index index.html;
        }
    }
复制代码

http, server, location关系:

http: 解决用户请求中的报文信息。

server: 配置具体某个网站的响应操作内容。

location: 配置url。

 Nginx部署多个站点,多个实例

 

posted @   Peter.Jones  阅读(26)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
点击右上角即可分享
微信分享提示