简介
作用
- 可直接支持Rails和PHP程序
- 可作为HTTP反向代理服务器
- 作为负载均衡服务器
- 作为邮件代理服务器
- 帮助实现前端动静分离
常用命令
- 启动
/usr/local/nginx/sbin/nginx
- 停止
/usr/local/nginx/sbin/nginx -s stop 快速停止
/usr/local/nginx/sbin/nginx -s quit 完整有序的停止
- 重启
/usr/local/nginx/sbin/nginx -s reload
- 重新打开日志文件
/usr/local/nginx/sbin/nginx -s reopen
- 测试nginx配置文件是否正确
/usr/local/nginx/sbin/nginx -t -c /path/to/nginx.conf
代理配置
在nginx.conf中添加引入目录
vim /usr/local/nginx/conf/nginx.conf
……………………
include /root/lms/config/nginx/conf/*.conf;
# another virtual host using mix of IP-, name-, and port-based configuration
……………………
编写代理文件
# vim /root/lms/config/nginx/conf/xxxx.conf
server {
listen 80;
server_name xxxxx;
access_log /root/lms/config/nginx/logs/xxxxx.access.log;
root /root/lms/xxxxxx;
location / {
try_files $uri $uri/ @router;#需要指向下面的@router否则会出现vue的路由在nginx中刷新出现404
index index.html index.htm;
#转发到tomcat等服务应用
#proxy_redirect off;
#proxy_set_header Host $host;
#proxy_set_header X-Real-IP $remote_addr;
#proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#proxy_pass http://127.0.0.1:8090;
}
# 对应上面的@router,主要原因是路由的路径资源并不是一个真实的路径,所以无法找到具体的文件
# 因此需要rewrite到index.html中,然后交给路由在处理请求资源
location @router {
rewrite ^.*$ /index.html last;
}
#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;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
安装
# wget http://nginx.org/download/nginx-1.19.1.tar.gz
# tar -zxvf nginx-1.19.1.tar.gz
# cd nginx-1.19.1/
# ./configure && make && make install
# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
安装中出现的问题
现象描述
./configure: error: the HTTP rewrite module requires the PCRE library.
解决方案
1、yum -y install pcre-devel;若还出现,则继续
2、yum -y install openssl openssl-devel
现象描述
checking for OS
+ Linux 3.10.0-123.el7.x86_64 x86_64
checking for C compiler ... not found
./configure: error: C compiler cc is not found
解决方案:
执行yum install gcc-c++安装gcc
现象描述
./configure: error: the HTTP gzip module requires the zlib library.
You can either disable the module by using --without-http_gzip_module
option, or install the zlib library into the system, or build the zlib library
statically from the source with nginx by using --with-zlib=<path> option.
解决方案
出现该提示,表示缺少zlib库和PCRE库。执行yum install pcre;yum install pcre-devel;yum install zlib;yum install zlib-devel;
现象描述
cp: 'conf/koi-win' and '/usr/local/nginx/conf/koi-win' are the same file,
解决方案
如果该提示,可能是你把安装包解压到了/usr/local/nginx目录,解决办法是将该目录重命名为其他名称后再执行make,make install.