https://nginx.org/en/download.html
nginx -v 查看nginx的版本信息
nginx -V 除版本信息外还显示配置参数信息
nginx -t -c filename 测试指定nginx配置文件是否正确
nginx -t 测试所有nginx配置文件是否正确
ps -ef | grep nginx 查看nginx的进程ID
nginx -s stop 关闭nginx,快速停止nginx
nginx -s reload 重新载入nginx,当配置信息修改需要重新加载配置时使用
https://blog.csdn.net/weixin_55549435/article/details/131232151
https://blog.csdn.net/u010164190/article/details/111562849
tar -zxvf nginx-1.25.3.tar.gz
cd nginx-1.25.3
#初始化
./configure
#初始化
make
#安装
make install
[^./configure --prefix=~/home/out 可以设置安装位置{安装默认在/usr/local/目录下}]:
cd /usr/local/nginx/sbin
#启动nginx
./nginx
curl http://127.0.0.1
cd /etc/systemd/system
#创建
vim nginx.service
创建内容
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
修改权限
chmod 777 nginx.service
服务命令
启动nginx
systemctl start nginx.service
# 停止nginx
systemctl stop nginx.service
# 重启nginx
systemctl restart nginx.service
# 查看nginx运行状态
systemctl status nginx.service
常见错误
一个常见的错误
Warning: nginx.service changed on disk. Run 'systemctl daemon-reload' to reload units.
直接按照提示执行命令systemctl daemon-reload 即可。
执行: systemctl daemon-reload
** 一个常见的错误**
root@localhost ~]# systemctl status network.service
**Failed to get [properties](https://so.csdn.net/so/search?q=properties&spm=1001.2101.3001.7020): Access denied
systemctl enable nginx.service
cd /
#修改
vim /etc/profile
添加如下内容
PATH=$PATH:/usr/local/nginx/sbin
export PATH
刷新配置文件
source /etc/profile
测试
nginx -v
输出:nginx version: nginx/1.25.3
https://blog.csdn.net/qq_27384769/article/details/81217634
默认位置
/etc/nginx/nginx.conf
find 搜索文件位置
find / -name nginx.conf
#默认配置{定义Nginx运行的用户和用户组}
#user nobody;
user root;
#默认配置{建议设置为等于CPU总核心数}
worker_processes 1;
#理论值应该是最多打开文件数
worker_rlimit_nofile 100000;
#error_log是个主模块指令,用来定义全局错误日志文件,日志输出级别有[ debug | info | notice | warn | error | crit ]可供选择,其中debug输出日志最为详细,而crit输出日志最少
error_log /home/application/logback/nginx/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#默认配置{pid是主模块指令,用来指定进程id的存储文件位置}
#pid logs/nginx.pid;
#events指令用来设定nginx工作模式与连接数上限
events {
#默认配置{进程的最大连接数受linux系统的最大打开文件数限制}
worker_connections 1024;
}
http {
#默认配置{主模块指令,实现对配置文件所包含的文件设定,可以减少主配置文件的复杂度,类似apache中的include方法}
include mime.types;
#默认配置{属于http核心模块指令,这里默认类型为二进制流,也就是当文件类型未定义是使用这种方式,例如在没有配置php环境时,nginx是不予解析的,此时,用浏览器访问php文件就会出现下载窗口}
default_type application/octet-stream;
#隐藏ngnix版本号
server_tokens off;
#忽略不合法的请求头
ignore_invalid_headers on;
#默认配置{日志格式设定}
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 /home/application/logback/nginx/access.log;
#服务器名字的hash表大小
server_names_hash_bucket_size 128;
client_max_body_size 300M;
client_header_buffer_size 32m;
large_client_header_buffers 4 32m;
#默认配置{开启高效文件传输模式,将tcp_nopush和tcp_nodely两个指令设置为on,用于防止网络阻塞}
sendfile on;
#默认配置{防止网络阻塞}
#tcp_nopush on;
#默认配置{用于设置客户端连接保持活动的超时时间,在超过这个时间之后服务器会关闭该链接}
keepalive_timeout 65;
#设置头部哈希表的最大值,不能小于你后端服务器设置的头部总数
proxy_headers_hash_max_size 51200;
#设置头部哈希表大小
proxy_headers_hash_bucket_size 6400;
#默认不开启
#gzip on;
#gzip模块设置,使用 gzip 压缩可以降低网站带宽消耗,同时提升访问速度。
gzip on; #开启gzip
gzip_min_length 1k; #最小压缩大小
gzip_buffers 4 16k; #压缩缓冲区
gzip_http_version 1.1; #压缩版本
gzip_comp_level 4; #压缩等级
gzip_types text/javascript text/plain application/x-javascript text/css application/xml application/json; #压缩类型
#虚拟主机的配置
server {
#默认配置{listen指定虚拟主机服务器端口}
listen 80;
#默认配置{用来指定ip地址或者域名,多个域名之间用空格分开}
server_name localhost;
#默认配置{charser用于设置网页的默认编码格式}
#charset koi8-r;
#默认配置{用来指定此虚拟机的访问日志存放路径}
#access_log /home/application/logback/nginx/host.access.log;
#error_log 用来指定此虚拟机的错误日志存放路径
#error_log /home/application/logback/nginx/goods.error.log;
#index用于设定访问的默认首页地址
#index index.shtml index.html index.htm;
#root指令用于指定虚拟主机的网页根目录,这个目录可以是相对路径,也可以使绝对路径
#root /usr/local/nginx/html;
location / {
root html;
index index.html index.htm;
}
location /jwcms {
proxy_pass http://124.221.88.127:8080/jwcms;
proxy_redirect default;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#expires -1;
index index.html index.htm index.shtml;
#代理静态资源文件
location ~ .*\.(gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ {
proxy_pass http://124.221.88.127:8080;
#expires缓存时间设置用来指定过期时间
expires 30d;
index index.html index.htm index.shtml;
}
# 支持WebSocket start
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
# 支持WebSocket end
}
#CMS预览
location /preview {
#root相对路径,alias绝对路径
alias /home/application/CMSData/preview/;
proxy_set_header Host $Host;
proxy_set_header X-Forwarded-For $remote_addr;
#expires缓存时间设置用来指定过期时间
expires -1;
index index.html index.htm index.shtml;
}
#CMS发布
location /pub {
#root相对路径,alias绝对路径
alias /home/application/CMSData/pub/;
proxy_set_header Host $Host;
proxy_set_header X-Forwarded-For $remote_addr;
client_max_body_size 30M;
#expires缓存时间设置用来指定过期时间
expires -1;
index index.html index.htm index.shtml;
}
#CMS图片
location /webpic {
#root相对路径,alias绝对路径
alias /home/application/CMSData/webpic/;
proxy_set_header Host $Host;
proxy_set_header X-Forwarded-For $remote_addr;
client_max_body_size 30M;
#expires缓存时间设置用来指定过期时间
#expires -1;
index index.html index.htm index.shtml;
}
#CMS附件
location /upload {
#root相对路径,alias绝对路径
alias /home/application/CMSData/upload/;
proxy_set_header Host $Host;
proxy_set_header X-Forwarded-For $remote_addr;
client_max_body_size 30M;
#expires缓存时间设置用来指定过期时间
#expires -1;
index index.html index.htm index.shtml;
}
#CMS临时目录
location /systemp {
#root相对路径,alias绝对路径
alias /home/application/CMSData/systemp/;
proxy_set_header Host $Host;
proxy_set_header X-Forwarded-For $remote_addr;
client_max_body_size 30M;
#expires缓存时间设置用来指定过期时间
#expires -1;
index index.html index.htm index.shtml;
}
#CMS临时目录
location /usertemp {
#root相对路径,alias绝对路径
alias /home/application/CMSData/usertemp/;
proxy_set_header Host $Host;
proxy_set_header X-Forwarded-For $remote_addr;
client_max_body_size 30M;
#expires缓存时间设置用来指定过期时间
#expires -1;
index index.html index.htm index.shtml;
}
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 html;
}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
location /jwcms {
proxy_pass http://124.221.88.127:8080/jwcms;
proxy_redirect default;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#expires -1;
index index.html index.htm index.shtml;
# 支持WebSocket start
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
# 支持WebSocket end
}