shaoQiz
shaoQiz
发布于 2025-09-23 / 35 阅读
0
0

Nginx安装和使用

一、安装Nginx

官网地址

https://nginx.org/en/download.html

安装

nginx-1.28.0.tar.gz

# 源码下载与解压
wget https://nginx.org/download/nginx-1.28.0.tar.gz
tar -zxvf nginx-1.28.0.tar.gz
cd nginx-1.28.0/
# 装编译工具及开发库
yum install -y gcc pcre-devel zlib-devel openssl-devel
# 编译配置与安装
./configure --prefix=/opt/nginx --with-http_ssl_module --with-http_v2_module
make && sudo make install

二、Nginx使用

Nginx常用命令

cd /opt/nginx/sbin/
./nginx	#启动
./nginx -s stop	#停止
./nginx -s quit	#安全退出
./nginx -s reload	#重新加载配置文件

Nginx配置文件

# 指定运行Nginx的用户和用户组(默认为nobody)
#user  nobody;

# 指定工作进程数(通常设置为CPU核心数)
worker_processes  1;

# 错误日志配置(可指定路径和日志级别)
#error_log  logs/error.log;        # 基础错误日志
#error_log  logs/error.log  notice; # 包含通知级别的日志
#error_log  logs/error.log  info;   # 包含信息级别的日志

# 指定PID文件存储路径(用于管理进程)
#pid        logs/nginx.pid;

# 事件模块配置
events {
    # 单个工作进程允许的最大连接数
    worker_connections  1024;
}

# HTTP核心模块配置
http {
    # 包含MIME类型定义文件
    include       mime.types;
    
    # 默认MIME类型
    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        on;
    
    # 启用TCP_NOPUSH选项(在sendfile开启时有效,减少网络包数量)
    #tcp_nopush     on;

    # 客户端连接保持超时时间(秒)
    #keepalive_timeout  0;  # 禁用长连接
    keepalive_timeout  65; # 65秒超时

    # 启用GZIP压缩(已注释)
    #gzip  on;

    # 定义虚拟主机(HTTP服务)
    server {
        # 监听端口和地址(默认监听所有IP的80端口)
        listen       80;
        
        # 域名绑定(支持通配符和正则)
        server_name  localhost;

        # 字符集设置
        #charset koi8-r;

        # 虚拟主机访问日志
        #access_log  logs/host.access.log  main;

        # 根路径配置
        location / {
            # 网站根目录
            root   html;
            
            # 默认首页文件
            index  index.html index.htm;
        }

        # 404错误页面配置
        #error_page  404              /404.html;

        # 服务器错误页面重定向
        error_page   500 502 503 504  /50x.html;
        
        # 精确匹配50x.html的处理
        location = /50x.html {
            root   html; # 错误页面存放目录
        }

        # PHP脚本代理到Apache
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # PHP脚本通过FastCGI处理
        #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;
        #}

        # 禁止访问.htaccess文件
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

    # 多端口/多域名虚拟主机示例
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

    # HTTPS服务器配置示例
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;        # SSL证书文件
    #    ssl_certificate_key  cert.key;       # SSL私钥文件

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


评论