nginx安装

打开网易新闻 查看更多图片

系统:centos7.8 (ip地址:192.168.10.200)

Nginx版本:nginx-1.20.2 (稳定版)

Nginx下载地址:http://nginx.org/en/download.html

上传安装包到服务器/opt目录,并进行解压安装:

#安装nginx依赖包

  • yum -y install gcc gcc-c++ zlib-devel pcre-devel openssl-devel

#解压

  • tar -zxvf nginx-1.20.2.tar.gz

#编译安装,检查环境,设置安装参数,这里使用默认方式

  • ./configure
  • make
  • mkae install

#安装完成标志如下图。

#启动nginx

  • cd /usr/local/nginx/sbin/
  • ./nginx

访问:http://192.168.10.200

打开网易新闻 查看更多图片

注:如果无法访问到以上页面,请检查:

(1)nginx进程是否正常(ps -ef |grep nginx);

(2)检查防火墙是否开放80端口(iptables -nvL --line)。

Nginx相关目录:

默认安装目录:/usr/local/nginx

默认网站根目录:/usr/local/nginx/html

默认配置文件目录:/usr/local/nginx/conf/nginx.conf

nginx常用命令

编译安装需要进入如下目录执行相关命令:

  • cd /usr/local/nginx/sbin/

#启动

  • ./nginx

#停止

  • ./nginx -s stop

#安全退出

  • ./nginx -s quit

#重新加载配合文件(平滑重启)

  • ./nginx -s reload

#测试配置文件是否有语法错误

  • ./nginx -t

#查看nginx进程

  • ps aux|grep nginx

nginx配置文件详解

more /usr/local/nginx/conf/nginx.conf

#启动nginx工作进程的用户和组

#user nobody;

#启动的工作进程数

worker_processes 1;

#nginx错误日志配置

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid文件保存路径

#pid logs/nginx.pid;

#events设置块

#设置单个nginx工作进程可以接受的最大并发连接数据;

#在nginx作为http服务器的时候,最大连接数为(进程数* 最大连接数);

#在nginx作为反向代理服务器的时候,最大连接数为(进程数* 最大连接数/2)

events {
worker_connections 1024;

#http设置块

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;

#指定是否使用sendfile系统调用来传输文件

sendfile on;

#在开启了sendfile的情况下,合并请求后统一发送给客户端

#tcp_nopush on;

#设置会话保持时间,单位是秒

#keepalive_timeout 0;
keepalive_timeout 65;

#开启文件压缩

#gzip on;

#server模块

server {

#设置监听端口

listen 80;

#设置server name,可以以空格隔开写多个,支持正则表达式

server_name localhost;

#指定网站目录

root /var/www/;

#指定默认网页文件,此指令由ngx_http_index_module模块提供

index index.html index.htm;

#设置编码格式,默认是俄语格式,可以改为utf-8

#charset koi8-r;

#设置访问日志

#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;

#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;

#以http的方式转发php请求到指定web服务器

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;

#以fastcgi的方式转发php请求到php处理

# 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;

#拒绝web形式访问指定文件,如很多的网站都是通过.htaccess文件来改变自己的重定向等功能。

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#location ~ /\.ht {
# deny all;


# another virtual host using mix of IP-, name-, and port-based configuration

#自定义虚拟server

#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }

#https服务器配置

# 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;
# }