本文转载自:https://www.cnblogs.com/huihui-hui/p/14544034.html
安装nginx相关依赖软件
1.选定源码目录
选定目录 /usr/local/
cd /usr/local/
2.安装PCRE库
yum -y install pcre-devel
3.安装zlib库
cd /usr/local/
wget http://www.zlib.net/zlib-1.2.11.tar.gz
tar -xvf zlib-1.2.11.tar.gz
cd zlib-1.2.11
./configure
make
make install
4.安装ssl(如果不需要ssl配置可忽略)
cd /usr/local/
wget https://www.openssl.org/source/openssl-1.0.2t.tar.gz
tar -xvf openssl-1.0.2t.tar.gz
cd openssl-1.0.2t
./config
make
make install
5.安装nginx,nginx版本会不断更新,自行参考官网最新版本
cd /usr/local/
wget http://nginx.org/download/nginx-1.14.0.tar.gz
tar -xvf nginx-1.14.0.tar.gz
cd nginx-1.14.0/
./configure --prefix=/usr/local/nginx
make
make insta
使用 /usr/local/nginx/sbin/nginx -t可查看应用nginx.conf的位置
6.the HTTP rewrite module requires the PCRE library报错,如果没有报错跳过
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.
安装pcre-devel解决问题
yum -y install pcre-devel
出现以下错误
src/http/ngx_http_script.c:698:18: error: cast between incompatible function types from ‘size_t (*)(ngx_http_script_engine_t *)’ {aka ‘long unsigned int (*)(struct <anonymous> *)’} to ‘void (*)(ngx_http_script_engine_t *)’ {aka ‘void (*)(struct <anonymous> *)’} [-Werror=cast-function-type]
698 | code->code = (ngx_http_script_code_pt) ngx_http_script_copy_len_code;
| ^
src/http/ngx_http_script.c: In function ‘ngx_http_script_add_var_code’:
src/http/ngx_http_script.c:787:18: error: cast between incompatible function types from ‘size_t (*)(ngx_http_script_engine_t *)’ {aka ‘long unsigned int (*)(struct <anonymous> *)’} to ‘void (*)(ngx_http_script_engine_t *)’ {aka ‘void (*)(struct <anonymous> *)’} [-Werror=cast-function-type]
787 | code->code = (ngx_http_script_code_pt) ngx_http_script_copy_var_len_code;
解决:进入Makefile
vim /usr/local/nginx-1.14.0/objs/Makefile
在文件中找到:CFLAGS =-pipe -0 -w -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g
把 -Werror 删掉,然后保存退出后,重新make
7.启动nginx
确保系统的 80 端口没被其他程序占用
启动
/usr/local/nginx/sbin/nginx
检查是否启动成功:
netstat -ano|grep 80
有结果输入说明启动成功
打开浏览器访问此机器的 IP,如果浏览器出现 Welcome to nginx! 则表示 Nginx 已经安装并运行成功。
8.重启nginx
/usr/local/nginx/sbin/nginx –s reload
9.修改配置文件
cd /usr/local/nginx/conf
vi nginx.conf
nginx.conf内容修改如下:
user root; #这步是防止静态资源权限提示503
worker_processes 1;
error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include 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 logs/access.log main;
underscores_in_headers on;
sendfile on;
keepalive_timeout 65;
client_max_body_size 80m;
include vhost/*.conf;
}
在conf目录创建vhost文件夹,放各个项目或者域名的配置,所以这里用到
include vhost/*.conf;
cd vhost
vi test.conf
test.conf内容:
server {
listen 80;
server_name www.test.com;
server_name_in_redirect off;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
fastcgi_connect_timeout 1200;
location /test-admin {
#解决跨域问题
add_header Access-Control-Allow-Methods *; add_header Access-Control-Max-Age 3600; add_header Access-Control-Allow-Credentials true; add_header Access-Control-Allow-Origin $http_origin;#获取请求端的origin值 add_header Access-Control-Allow-Headers $http_access_control_request_headers;#这里的请求变量必须都是小写的 if ($request_method = OPTIONS){#这里的if后面要带空格的,不然语法会错 return 200; }
proxy_pass http://127.0.0.1:6001/test-admin;
}
location /test-file/ {
alias /home/test/test-file/;
}
}
#如无需配置SSL证书,请忽略以下配置
server {
listen 443;
server_name www.test.com; #填写绑定证书的域名
ssl on; #1.21.5版本需将该行注释
ssl_certificate ssl/5334682_www.test.com.pem; # hps文件夹下的.crt文件
ssl_certificate_key ssl/5334682_www.test.com.key; #hps文件夹下的.key文件
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #按照这个协议配置
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; #按照这个套件配置
ssl_prefer_server_ciphers on;
location /test-admin {
proxy_pass http://127.0.0.1:6001/test-admin;
}
location /test-file/ {
alias /home/test/test-file/;
}
}#现在nginx新版SSL配置,如下:
server {
listen 443 ssl;
server_name www.test.com; #填写绑定证书的域名
ssl_certificate ssl/5334682_www.test.com.pem; # hps文件夹下的.crt文件
ssl_certificate_key ssl/5334682_www.test.com.key; #hps文件夹下的.key文件
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #按照这个协议配置
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; #按照这个套件配置
ssl_prefer_server_ciphers on;
location /test-admin {
proxy_pass http://127.0.0.1:6001/test-admin;
}
location /test-file/ {
alias /home/test/test-file/;
}
}
在conf目录下创建ssl文件夹,专门储存申请的SSL证书
10、配置SSL证书(无需配置SSL证书,请忽略以下步骤)
由于我们未装SSL模块,启动时,会提示nginx:[emerg]unknown directive ssl错误
因为我们配置这个SSL证书需要引用到nginx的中SSL这模块,然而我们一开始编译的Nginx的时候并没有把SSL模块一起编译进去,所以导致这个错误的出现。
解决方案:
步骤一:我们先来到当初下载nginx的包压缩的解压目录,如果你是看小编写的教程安装的,解压目录应该在“/usr/loacl/nginx-1.14.0/”,绝大多数应该都是在这个目录下的。
步骤二:来到解压目录下后,按顺序执行一下命令:
命令1、./configure --with-http_ssl_module //重新添加这个ssl模块
注意如果没有出现错误,则直接看命令2即可
执行以上一条命令出现这个错误(./configure:错误:SSL模块需要OpenSSL库。),原因是因为缺少了OpenSSL,那我们再来安装一个即可执行:yum -y install openssl openssl-devel
等待OpenSSL的安装完成后,再执行./configure --with-http_ssl_module即可
注:如果还提示OpenSSL未安装,可参考该文章下载源码安装https://blog.csdn.net/qq_29235677/article/details/122083300,原理就是下载源码安装,在./configure编译带上OpenSSL源码路径,例如
./configure --with-http_ssl_module --with-openssl=/root/openssl-1.1.1g,这里/root/openssl-1.1.1g看你源码放在哪里
命令2、执行make命令,但是不要执行make install,因为make是用来编译的,而make install是安装,不然你整个nginx会重新覆盖的。
命令3、在我们执行完做命令后,我们可以查看到在nginx解压目录下,objs文件夹中多了一个nginx的文件,这个就是新版本的程序了。首先我们把之前的nginx先备份一下,然后把新的程序复制过去覆盖之前的即可。
cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
cp objs/nginx /usr/local/nginx/sbin/nginx
输入y同意覆盖原先文件
命令4,最后我们来到Nginx安装目录下,来查看是否有安装ssl模块成功。执行./sbin/nginx -V即可
最后出现configure arguments: --with-http_ssl_module 即表示成功
由于ssl端口为443,需要确保服务器此端口开放才可正常访问
出现Nginx的405 not allowed错误解决【Nginx静态文件响应POST请求,提示405错误问题】
可参考改文章解决
https://www.zuopeng.gd.cn/article/details/id/116.html