Linux ❀ Nginx基础解释与服务部署搭建
Posted 国家级干饭型选手°
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux ❀ Nginx基础解释与服务部署搭建相关的知识,希望对你有一定的参考价值。
1、基础概念
2、安装nginx
查看Nginx RPM安装包信息;
[root@localhost ~]# yum list | grep nginx
nginx.x86_64 1:1.14.1-9.module+el8.0.0+4108+af250afe @AppStream
nginx-all-modules.noarch 1:1.14.1-9.module+el8.0.0+4108+af250afe @AppStream
nginx-filesystem.noarch 1:1.14.1-9.module+el8.0.0+4108+af250afe @AppStream
nginx-mod-http-image-filter.x86_64 1:1.14.1-9.module+el8.0.0+4108+af250afe @AppStream
nginx-mod-http-perl.x86_64 1:1.14.1-9.module+el8.0.0+4108+af250afe @AppStream
nginx-mod-http-xslt-filter.x86_64 1:1.14.1-9.module+el8.0.0+4108+af250afe @AppStream
nginx-mod-mail.x86_64 1:1.14.1-9.module+el8.0.0+4108+af250afe @AppStream
nginx-mod-stream.x86_64 1:1.14.1-9.module+el8.0.0+4108+af250afe @AppStream
pcp-pmda-nginx.x86_64 5.0.2-5.el8 AppStream
使用yum命令安装Nginx;
[root@localhost ~]# yum install -y nginx
Complete!
查看Nginx安装包具体版本信息与配置文件信息;
[root@localhost ~]# rpm -qa nginx
nginx-1.14.1-9.module+el8.0.0+4108+af250afe.x86_64
[root@localhost ~]# rpm -qc 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
/etc/nginx/mime.types.default
/etc/nginx/nginx.conf /主配置文件;
/etc/nginx/nginx.conf.default
/etc/nginx/scgi_params
/etc/nginx/scgi_params.default
/etc/nginx/uwsgi_params
/etc/nginx/uwsgi_params.default
/etc/nginx/win-utf
查看服务进程是否启动成功
[root@localhost ~]# ps -ef | grep nginx
root 36142 1 0 22:41 ? 00:00:00 nginx: master process /usr/sbin/nginx
nginx 36143 36142 0 22:41 ? 00:00:00 nginx: worker process
nginx 36144 36142 0 22:41 ? 00:00:00 nginx: worker process
nginx 36145 36142 0 22:41 ? 00:00:00 nginx: worker process
nginx 36146 36142 0 22:41 ? 00:00:00 nginx: worker process
root 36293 7571 0 22:53 pts/0 00:00:00 grep --color=auto nginx
关闭防火墙并关闭Selinux
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld
[root@localhost ~]# setenforce 0 && sed -i 's/^SELINUX=.*/SELINUX=disable/g' /etc/selinux/config
使用浏览器打开:http://[ip],即可访问到如下Nginx欢迎页面
也可使用curl命令进行访问该页面(返回信息为html编码页面,不建议使用此验证方式)
[root@localhost ~]# curl http://10.81.20.167
#10.81.20.167为Nginx服务所在服务器IP地址;
3、Nginx主配置文件
主配置文件:
[root@localhost ~]# cat /etc/nginx/nginx.conf
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
#---------------------------------------------------------------------
#全局块;从配置文件开始到events块之间的内容,主要会设置一些影响nginx服务整体运行的配置指令;
user nginx;
worker_processes auto;
#nginx并发处理的值,值越大支持处理的并发数量越多
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
#---------------------------------------------------------------------
#events块;主要影响Nginx服务与用户的网络连接;
events {
worker_connections 1024;
#支持的最大连接数;
}
#---------------------------------------------------------------------
#http全局块;
http {
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;
tcp_nodelay on;
keepalive_timeout 65;
#连接超时时间;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
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.
include /etc/nginx/conf.d/*.conf;
#接入子配置文件目录下的配置文件,后续可以对此文件进行修改即可完成对Nginx服务的修改,同HTTP服务配置文件的修改;
#---------------------------------------------------------------------
#server块;
server {
listen 80 default_server;
listen [::]:80 default_server;
#默认监听端口为80;
server_name _;
#服务名称;
root /usr/share/nginx/html;
#默认访问网页存放路径(即使用浏览器默认访问到的页面);
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
#当访问的路径中包含“/”时,需要执行{ }内的指令;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
# Settings for a TLS enabled server.
#设置HTTPS访问页面配置参数;
# server {
# listen 443 ssl http2 default_server;
# listen [::]:443 ssl http2 default_server;
# 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 PROFILE=SYSTEM;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# location / {
# }
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
}
4、Nginx常用命令
安装Nginx服务会使得添加新命令,命令PATH如下:
[root@localhost ~]# type nginx
nginx is hashed (/usr/sbin/nginx)
查看Nginx版本信息
[root@localhost ~]# nginx -v
nginx version: nginx/1.14.1
关闭服务
[root@localhost ~]# nginx -s stop
[root@localhost ~]# ps -ef | grep nginx
root 37026 7571 0 23:11 pts/0 00:00:00 grep --color=auto nginx
启动服务
[root@localhost ~]# nginx
[root@localhost ~]# ps -ef | grep nginx
root 37036 1 0 23:12 ? 00:00:00 nginx: master process nginx
nginx 37037 37036 0 23:12 ? 00:00:00 nginx: worker process
nginx 37038 37036 0 23:12 ? 00:00:00 nginx: worker process
nginx 37039 37036 0 23:12 ? 00:00:00 nginx: worker process
nginx 37040 37036 0 23:12 ? 00:00:00 nginx: worker process
root 37042 7571 0 23:12 pts/0 00:00:00 grep --color=auto nginx
重启服务
[root@localhost ~]# nginx -s reload
由于此服务由RPM包安装完成,因此也可以使用如下命令完成服务的开启、关闭、重启、状态查询、开启是否自启;
[root@localhost ~]# systemctl
add-requires get-default list-machines revert
add-wants halt list-sockets set-default
cancel help list-timers set-environment
cat hibernate list-unit-files set-property
condreload hybrid-sleep list-units show
condrestart import-environment mask show-environment
condstop is-active poweroff start
daemon-reexec is-enabled preset status
daemon-reload is-failed preset-all stop
default isolate reboot suspend
disable is-system-running reenable suspend-then-hibernate
edit kexec reload switch-root
emergency kill reload-or-restart try-reload-or-restart
enable link rescue try-restart
exit list-dependencies reset-failed unmask
force-reload list-jobs restart unset-environment
[root@localhost ~]# systemctl start nginx
以上是关于Linux ❀ Nginx基础解释与服务部署搭建的主要内容,如果未能解决你的问题,请参考以下文章
Plasm部署智能合约——nginx反代转发plasm节点服务——2021.6.2