文章目录
- 简介
- 安装
- Ubuntu安装
- CentOS安装
- 常用命令
- 配置文件 nginx.conf
- 项目测试:Hexo博客
- 三、配置自己监听服务
简介
- Web服务器,目的是解决高并发问题
- C语言开发
- 启动后浏览器输入 http://localhost/ 显示欢迎页面就是启动成功了。
- 在nginx安装目录下cmd就可以进入命令行了
- conf:存在Nginx配置文件的目录
- Nginx的配置文件是在conf目录下的nginx.conf文件
- docs:存放Nginx文档的目录
- html:存放静态html文件的目录
- logs:存放Nginx日志的目录
- temp:存放临时文件的目录
安装
Ubuntu安装
# 1. 更新仓库信息
sudo apt-get update
# 2. 安装nginx
sudo apt-get install nginx
# 3. 验证安装
sudo nginx -V
CentOS安装
CentOS系Linux发⾏版可以使⽤yum来安装。
# 1. 安装EPEL仓库
sudo yum install epel-release
# 2. 更新repo
sudo yum update
# 3. 安装nginx
sudo yum install nginx
# 4. 验证安装
sudo nginx -V
常用命令
- windows:如果你没有把该目录加入到系统的path中,请在nginx当前目录下,打开cmd命令行,然后输入命令。
- linux直接打开终端即可。
查看nginx的版本号
nginx -v
查看nginx详细信息
nginx -V(大写)
启动服务
nginx
没有提示信息就表明已经启动了。
打开浏览器输入localhost显示nginx欢迎页面表示成功启动。
查看进程
ps -ef | grep nginx
root 3572 1 0 10:59 ? 00:00:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
www-data 3574 3572 0 10:59 ? 00:00:00 nginx: worker process
www-data 3575 3572 0 10:59 ? 00:00:00 nginx: worker process
www-data 3576 3572 0 10:59 ? 00:00:00 nginx: worker process
www-data 3577 3572 0 10:59 ? 00:00:00 nginx: worker process
www-data 3578 3572 0 10:59 ? 00:00:00 nginx: worker process
www-data 3579 3572 0 10:59 ? 00:00:00 nginx: worker process
root 7787 4311 0 11:29 pts/1 00:00:00 grep --color=auto nginx
master 只有一个。表示主进程,负责读取与验证配置文件,管理worker进程
worker 可以多个。是实际工作进程,
查看端口占用情况
lsof -i:80
重载配置
nginx -s reload
快速停止或关闭
nginx -s stop
正常停止或关闭
nginx -s quit
重新打开日志文件
nginx -s reopen
配置文件 nginx.conf
输入nginx -V查看详细信息
----------------------------------------------------
----------------------------------------------------
这是展示样例,里面都是nginx的信息
[root@yx-virtual-machine /] # nginx -V
nginx version: nginx/1.18.0 (Ubuntu)
built with OpenSSL 3.0.2 15 Mar 2022
TLS SNI support enabled
configure arguments: --with-cc-opt='-g -O2 -ffile-prefix-map=/build/nginx-zctdR4/nginx-1.18.0=. -flto=auto -ffat-lto-objects -flto=auto -ffat-lto-objects -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -flto=auto -ffat-lto-objects -flto=auto -Wl,-z,relro -Wl,-z,now -fPIC' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --modules-path=/usr/lib/nginx/modules --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-compat --with-debug --with-pcre-jit --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_v2_module --with-http_dav_module --with-http_slice_module --with-threads --add-dynamic-module=/build/nginx-zctdR4/nginx-1.18.0/debian/modules/http-geoip2 --with-http_addition_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_sub_module
----------------------------------------------------
----------------------------------------------------
寻找里面的--conf-path=/etc/nginx/nginx.conf 即是配置文件的位置
通过cat命令打开文件
cat /etc/nginx/nginx.conf
----------------------------------------------------
-----------------------------------------------------
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
#mail {
# # See sample authentication script at:
# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
# # auth_http localhost/auth.php;
# # pop3_capabilities "TOP" "USER";
# # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
# server {
# listen localhost:110;
# protocol pop3;
# proxy on;
# }
#
# server {
# listen localhost:143;
# protocol imap;
# proxy on;
# }
#}
----------------------------------------------------
----------------------------------------------------
# 匹配输入的URL
location / {
# root是关键字,html指的是nginx安装目录下的html文件夹,里面放项目。
# 你也可以指定其他文件夹放项目,只要在nginx文件夹以内就好
root html;
# index是关键字,表示默认打开的网址
# 后面两个是打开的网页文件的名字,第一个能用就不打开第二个了。
index index.html index.htm;
}
项目测试:Hexo博客
在这里插入代码片
三、配置自己监听服务
该配置文件的下面有个实例配置,被注释了。 配置自己的监听,可以参考这个。
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
下面我配置一个监听域名为:blog.test,端口也是80端口的配置:
# 监听本地域名为blog.test,端口为80
server {
listen 80;
server_name blog.test;
location / {
root C:\WWW\blog;
index index.html index.htm;
}
}
我的网站根目录在C:\WWW\blog
,里面有一个index.html
文件。改动之后记得重新加载配置:
nginx -s reload
在浏览器输入http://blog.test
出现如下页面: