install nginx
下载源码
http://nginx.org/en/download.html
wget http://nginx.org/download/nginx-1.21.5.tar.gz
解压
tar -zxvf nginx-1.21.5.tar.gz
下载依赖
软件包 | 说明 |
---|---|
pcre | 为 Nginx 模块提供正则表达式 |
zlib | 为 Nginx 模块提供数据压缩用的函数库 |
openssl | 为 Nginx 模块提供密码算法\证书\SSL 协议等功能 |
sudo apt-cache search pcre | grep -- -dev
sudo apt-get install openssl libssl-dev
sudo apt-get install libpcre3 libpcre3-dev
sudo apt-get install zlib1g-dev
编译安装
cd nginx-1.21.5
配置编译选项
./configure \
--prefix=/usr/local/nginx \
--with-http_ssl_module
在 Nginx 安装中还有很多模块,当用到的时候使用 --with
选项添加需要的模块重新编译安装即可.
Configuration summary
+ using system PCRE library
+ using system OpenSSL library
+ using system zlib library
nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx modules path: "/usr/local/nginx/modules"
nginx configuration prefix: "/usr/local/nginx/conf"
nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/nginx/logs/error.log"
nginx http access log file: "/usr/local/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp
编译安装
make && make install
配置环境变量
vim /etc/profile
export PATH=$PATH:/usr/local/nginx/sbin
source /etc/profile
修改配置文件
#user nobody;
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;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
# server {
# listen 80;
# server_name localhost;
#
# #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;
# }
#
# # 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;
# #}
# }
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# 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;
# }
#}
include /etc/nginx/conf.d/*.conf; # 此版本没有了这一行,手动添加,并注释相关的虚拟主机配置
}
配置虚拟主机
- 计时
upstream view{
server 127.0.0.1:8080; # 本地服务端口
}
server{
listen 80;
server_name view.binny.work;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Nginx-Proxy true;
proxy_pass http://view; # 代理转发
proxy_redirect off;
}
}
- 笔记
upstream note{
server 127.0.0.1:8000; # 本地服务端口
}
server{
listen 80;
server_name note.binny.work;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Nginx-Proxy true;
proxy_pass http://note; # 代理转发
proxy_redirect off;
}
}
手册
root@VM-0-16-ubuntu:~# nginx -h
nginx version: nginx/1.21.5
Usage: nginx [-?hvVtTq] [-s signal] [-p prefix]
[-e filename] [-c filename] [-g directives]
Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit
-t : test configuration and exit
-T : test configuration, dump it and exit
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen, reload
-p prefix : set prefix path (default: /usr/local/nginx/)
-e filename : set error log file (default: logs/error.log)
-c filename : set configuration file (default: conf/nginx.conf)
-g directives : set global directives out of configuration file
启动
nginx
root@VM-0-16-ubuntu:~# netstat -tpln
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 172.17.0.16:53 0.0.0.0:* LISTEN 686/named
tcp 0 0 127.0.0.1:53 0.0.0.0:* LISTEN 686/named
tcp 0 0 127.0.0.53:53 0.0.0.0:* LISTEN 654/systemd-resolve
tcp 0 0 127.0.0.1:953 0.0.0.0:* LISTEN 686/named
tcp 0 0 0.0.0.0:1228 0.0.0.0:* LISTEN 12799/sshd: /usr/sb
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/init
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 244573/nginx: maste
tcp6 0 0 fe80::5054:ff:fe05:9:53 :::* LISTEN 686/named
tcp6 0 0 ::1:53 :::* LISTEN 686/named
tcp6 0 0 ::1:953 :::* LISTEN 686/named
tcp6 0 0 :::111 :::* LISTEN 1/init
停止
立即停止
nginx -s stop
从容停止
nginx -s quit
常用命令
命令 | 说明 |
---|---|
nginx -s reload | 在 Nginx 启动的情况下重新加载配置文件,平滑启动 |
nginx -s reopen | 重新打开配置文件 |
nginx -c 配置文件路径 | 以特定目录下的配置文件启动 Nginx |
nginx -t | 检测当前配置文件是有正确 |
nginx -t -c 配置文件路径 | 检测特定目录下的配置文件是有正确 |
nginx -v | 显示版本信息 |
nginx -V | 显示版本信息和编译选项 |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)