centOS安装nginx及相关配置

服务器 寻梦 7年前 (2019-08-01) 3457次浏览 1个评论 扫描二维码
文章目录[隐藏]

下载安装

1.依赖项和必要组件

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

2.下载安装nginx

wget http://nginx.org/download/nginx-1.20.2.tar.gz
可以根据需要下载不同版本。官网:http://nginx.org/en/download.html

3.解压

tar zxvf nginx-1.20.2.tar.gz && cd nginx-1.20.2

4.编译配置

./configure && make && make install
执行完命令将会在 /usr/local/nginx 生成相应的可执行文件、配置、默认站点等文件

5.软连接创建全局命令

ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
(执行第五步后不需要去安装目录,不需要./)
./nginx :启动
./nginx -s stop(此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程)
./nginx -s quit (此方式停止步骤是待nginx进程处理任务完毕进行停止。)
./nginx -s reload 重启

配置

1.配置文件启动

/usr/local/nginx/conf/nginx.conf配置端口等,更改location的root目录

 

.conf配置文件的启动
在实际当中服务器中可能有多个vue前端项目,此时我们只要单独修该conf文件即可,一个前端项目对应的一个conf文件。
conf启动命令符如下:
启动项目指定配置文件 #nginx -c /usr/local/nginx/conf/nginx_hwfm.conf
查看启动进程: #ps -ef|grep nginx_hwfm
别忘了开服务器端口

nginx.conf配置文件详解

2.配置代理

upstream my_server {                                                         
    server 10.10.10.10:8080;                                                
    keepalive 2000;
}
server {
    listen       80;                                                         
    server_name  10.0.0.1;                                               
    client_max_body_size 1024M;

    location /my/ {
        proxy_pass http://my_server/abc/my/;
        proxy_set_header Host $host:$server_port;
    }
}
访问 http://10.10.10.10:8080/abc/my/.....
/my是唯一标识

配置linux 开机自启动

1.在/etc/init.d下创建文件nginx
 vim /etc/init.d/nginx 
 其内容参考nginx官方文档 
需要注意的配置:
 nginx=”/usr/local/nginx/nginx/sbin/nginx” //修改成nginx执行程序的路径。
 NGINX_CONF_FILE=”/usr/local/nginx/nginx/conf/nginx.conf” //修改成nginx.conf文件的路径。
# 保存后设置文件的执行权限
 chmod a+x /etc/init.d/nginx 
 # 至此就可以通过下面指令控制启动停止
/etc/init.d/nginx start
/etc/init.d/nginx stop 
#上面的方法完成了用脚本管理nginx服务的功能,但是还是不太方便。
先将nginx服务加入chkconfig管理列表:
chkconfig --add /etc/init.d/nginx 
# 加完这个之后,就可以使用service对nginx进行启动,重启等操作了。
service nginx start
service nginx stop
service nginx restart 
# 最后设置开机自动启动
chkconfig nginx on 

docker安装nginx

docker run -d -p 8080:80 --name eisp-web --network ads -v /var/lib/docker/volumes/nginx/eisp:/usr/share/nginx/html -v /var/lib/docker/volumes/nginx/conf/nginx.conf:/etc/nginx/nginx.conf nginx:latest

 

配置反向代理

 

mysql/tcp

# 默认编译时没有steam模块,编译时加上 --with-stream 
 ./configure  --with-stream  && make && make install
# 较为完整的编译
 ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module --with-http_flv_module --with-http_mp4_module --with-http_gzip_static_module --with-stream --with-stream_ssl_module --with-http_v2_module 

 源码路径进行编译后,就不再需要依赖系统中已安装的 依赖 库了

 ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module --with-http_flv_module --with-http_mp4_module --with-http_gzip_static_module --with-stream --with-stream_ssl_module --with-http_v2_module  --with-openssl=/usr/local/src/openssl/openssl-3.0.7 --with-pcre=/usr/local/src/pcre/pcre-8.45 --with-zlib=/usr/local/src/zlib/zlib-1.2.13 && make && make install

注意:这里的路径,不是指pcre,zlib,openssl的安装路径,而是源码文件的路径
当使用 with-pcre=/path/to/pcre 选项并指定 PCRE 库的源码路径时,在编译 Nginx 过程中会使用指定路径下的 PCRE 源码进行编译。这意味着在编译完成后,生成的 Nginx 可执行文件会包含 PCRE 库的代码,
并且不再依赖系统中已安装的 PCRE 库。
因此,当你指定了 PCRE 源码路径进行编译后,就不再需要依赖系统中已安装的 PCRE 库了。 Nginx 可执行文件中已经包含了 PCRE 库的代码,因此可以独立运行,而不需要外部的动态链接库。其他类似。

配置steam,steam要和http平级

# nginx 代理 mysql,监听本机3307端口,代理到xxx.com:3306
stream {
    # 设置代理超时时间,设的大一些,避免长连接因为超时时间而中断
    proxy_timeout 3d;
    
    server {
        listen 3307;
        listen [::]:3307;
        proxy_pass xxx.com:3306;
    }
}
# (方法2)nginx 代理 mysql,监听本机2188端口,代理到192.168.1.221:3306
stream {
  upstream mysql {
    zone myapp1 64k;
    server 192.168.1.221:3306 weight=1 max_fails=2 fail_timeout=30s;   
  }
  server {
        listen 2188;
        proxy_connect_timeout 1s;
        proxy_timeout 3s;
        proxy_pass mysql;
  }
}

常用配置

# 隐藏版本号
配置文件增加 server_tokens off;
# 413 Request Entity Too Large Nginx 错误
client_max_body_size 20m;

常见问题

1.  离线编译时报错:

Can't locate IPC/Cmd.pm in @INC (@INC contains: /opt/nginx-src/openssl-3.0.8/util/perl /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 . /opt/nginx-src/openssl-3.0.8/external/perl/Text-Template-1.56/lib) at /opt/nginx-src/openssl-3.0.8/util/perl/OpenSSL/config.pm line 19.
BEGIN failed--compilation aborted at /opt/nginx-src/openssl-3.0.8/util/perl/OpenSSL/config.pm line 19.
Compilation failed in require at /opt/nginx-src/openssl-3.0.8/Configure line 23.
BEGIN failed--compilation aborted at /opt/nginx-src/openssl-3.0.8/Configure line 23.
make[1]: *** [/opt/nginx-src/openssl-3.0.8/.openssl/include/openssl/ssl.h] 错误 2

解决方法:

yum -y install perl-IPC-Cmd

喜欢 (18)
[支付宝扫码,感谢支持]
分享 (0)
关于作者:

您必须 登录 才能发表评论!

(1)个小伙伴在吐槽