nginx安装
下载nginx:下载地址
上传到/opt目录
解压nginx,并进入解压后到目录
cd /opt
tar -zxvf nginx-1.25.2.tar.gz
cd nginx-1.25.2
编译(with-http_ssl_module为https模块)
./configure --with-http_ssl_module
安装
make install
默认的安装目录为:/usr/local/nginx
- 启动Nginx
./nginx
- 重启Nginx
./nginx -s reload
- 关闭Nginx
./nginx -s stop
生成https自签名证书,如果是公网域名,可以申请阿里云免费证书
创建证书目录,并进入该目录
mkdir /usr/local/nginx/cert
cd /usr/local/nginx/cert
生成私钥
openssl genrsa -out server.key 2048
生成公钥
openssl req -new -key server.key -out server.csr
生成证书
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
配置nginx https
vim /usr/local/nginx/conf/nginx.conf
添加以下内容
server {listen 443 ssl;server_name localhost;# https证书ssl_certificate /usr/local/nginx/cert/server.crt;ssl_certificate_key /usr/local/nginx/cert/server.key;ssl_session_cache shared:SSL:1m;ssl_session_timeout 5m;ssl_ciphers HIGH:!aNULL:!MD5;ssl_prefer_server_ciphers on;# 代理地址location / {proxy_pass http://localhost:8848/;}}
问题:nginx访问时报403错??
首先报错先查看日志,这里查看nginx日志,路径为/var/log/nginx/error.log。打开日志发现详细报错如下:
2022/12/22 16:08:06 [error] 16674#16674: *41 directory index of “/data/soft/” is forbidden, client: 58.250.63.15, server: server01, request: “GET / HTTP/1.1”, host: “xxxxxx:666”
没有权限?缺少web目录索引?还是… …?,下面这些问题都给你解决
报错的可能原因:
一、由于启动用户和nginx工作用户不一致所致
1.1查看nginx的启动用户
ps aux | grep "nginx: worker process" | awk'{print $1}'
1.2将nginx.config的user改为和启动用户一致
vim conf/nginx.conf
user nginx; #这里的用户改为与启动用户一致
worker_processes 8;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {worker_connections 65535;
}
二、缺少index.html或者index.php文件,就是配置文件中index index.html index.htm这行中的指定的文件。
server {listen 666;server_name server01;root /data/soft/;index index.html index.htm; #也可能是这里缺少了 。不过对于这次的报错,这里不影响
}
如果在/data/soft/下面没有index.php,index.html的时候,直接文件,会报403 forbidden。
三、配置文件里,少了一条参数:autoindex on
vim /etc/nginx/nginx.conf
http {include /etc/nginx/mime.types;default_type application/octet-stream;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;keepalive_timeout 65;autoindex on; #########就是少了这条参数,所以一直报 “41 directory index of "/data/soft/" is forbidden” 这个错autoindex_exact_size off;autoindex_localtime on;include /etc/nginx/conf.d/*.conf;
}
四、权限问题,如果nginx没有web目录的操作权限,也会出现403错误。
解决办法:修改web目录的读写权限,或者是把nginx的启动用户改成目录的所属用户,重启Nginx即可解决
chmod -R 777 /data/soft/
五、SELinux设置为开启状态(enabled)的原因。
5.1、查看当前selinux的状态。
/usr/sbin/sestatus
5.2、将SELINUX=enforcing 修改为 SELINUX=disabled 状态。
vim /etc/selinux/config
#SELINUX=enforcingSELINUX=disabled
重启生效,reboot。
reboot