首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >linux 安装配置Nginx

linux 安装配置Nginx

原创
作者头像
时光_赌徒
发布2024-11-13 11:41:14
发布2024-11-13 11:41:14
7690
举报
文章被收录于专栏:记录记录

1、安装相关依赖

代码语言:shell
复制
yum -y install gcc gcc-c++ wget net-tools pcre-devel zlib-devel openssl-devel

2、下载并解压安装包

代码语言:shell
复制
#进入常用文件夹 
cd /usr/local/src/ 

#下载源码 
wget http://nginx.org/download/nginx-1.26.2.tar.gz 

#解压 
tar zxvf nginx-1.26.2.tar.gz 

#进入目录 
cd nginx-1.26.2

3、配置并安装

3.1 配置
代码语言:shell
复制
#运行
./configure \
#安装目录
--prefix=/usr/local/nginx \ 
#启用线程支持
--with-threads \ 
#启用异步文件I/O(AIO)
--with-file-aio \ 
#启用PCRE支持。这允许Nginx使用正则表达式进行复杂的URL匹配和重写
--with-pcre \ 
#启用SSL支持
--with-http_ssl_module \ 
#启用通过HTTP头部重写客户端IP地址的功能。反向代理时获取真实客户端IP
--with-http_realip_module \ 
#启用向响应前后添加文本的功能
--with-http_addition_module \ 
#启用对响应内容进行修改(如替换文本)的功能
--with-http_sub_module \ 
#启用WebDAV(基于HTTP的远程文件访问协议)支持
--with-http_dav_module \ 
#启用对FLV(FlashVideo)文件的流式传输支持
--with-http_flv_module \ 
#启用对MP4文件的流式传输支持
--with-http_mp4_module \ 
#启用对gzip压缩内容的即时解压缩并传输给客户端的功能
--with-http_gunzip_module \ 
#启用直接传输预先gzip压缩的静态文件的功能
--with-http_gzip_static_module \ 
#启用基于子请求的授权功能
--with-http_auth_request_module \ 
#启用从目录中随机选择一个文件作为索引的功能
--with-http_random_index_module \ 
#启用基于密钥和过期时间的链接保护功能
--with-http_secure_link_module \ 
#启用在服务器负载较高时降低服务质量的策略,以维持服务可用性
--with-http_degradation_module \ 
#启用获取Nginx状态信息的模块,通常用于监控
--with-http_stub_status_module \ 
#启用邮件代理功能,使Nginx能够作为邮件代理服务器运行
--with-mail \ 
#启用邮件代理的SSL支持,为通过邮件服务器发送和接收的邮件提供加密
--with-mail_ssl_module
3.2 编译安装
  • 编译
代码语言:shell
复制
    make
  • 查看编译结果(显示0 表示编译成功)
代码语言:shell
复制
echo $?
  • 安装
代码语言:shell
复制
make install
  • 编写nginx启动脚本,并加入系统服务
代码语言:shell
复制
vim /etc/init.d/nginx

内容如下:

代码语言:shell
复制
 #!/bin/bash 
 # chkconfig: - 30 21 
 # description: http service. 
 # Source Function Library 
 . /etc/init.d/functions 
 # Nginx Settings
NGINX_SBIN="/usr/local/nginx/sbin/nginx"   NGINX_CONF="/usr/local/nginx/conf/nginx.conf" NGINX_PID="/usr/local/nginx/logs/nginx.pid" 
RETVAL=0 prog="Nginx" 

start() { 
	echo -n $"Starting $prog: " 
	mkdir -p /dev/shm/nginx_temp 
	daemon $NGINX_SBIN -c $NGINX_CONF 
	RETVAL=$? 
	echo 
	return $RETVAL 
} 
	
stop() { 
	echo -n $"Stopping $prog: " 
	killproc -p $NGINX_PID $NGINX_SBIN -TERM 
	rm -rf /dev/shm/nginx_temp 
	RETVAL=$? 
	echo 
	return $RETVAL 
} 
	
reload(){
	echo -n $"Reloading $prog: " 
	killproc -p $NGINX_PID $NGINX_SBIN -HUP 
	RETVAL=$? 
	echo 
	return $RETVAL 
}
	
restart(){ 
	stop 
	start 
} 

configtest(){ 
	$NGINX_SBIN -c $NGINX_CONF -t 
	return 0 
} 

case "$1" in 
	start) start 
			;; 
	stop) stop 
			;; 
	reload) reload 
			;; 
	restart) restart 
			;; 
	configtest) configtest 
			;; 
	*) 
			echo $"Usage: $0 {start|stop|reload|restart|configtest}" 
			RETVAL=1 
esac 
exit $RETVAL
  • 保存退出
代码语言:shell
复制
:wq
  • 添加权限
代码语言:shell
复制
chmod 755 /etc/init.d/nginx
  • 添加nginx到服务
代码语言:shell
复制
chkconfig --add nginx
  • 设置开机自启
代码语言:shell
复制
chkconfig nginx on

4、项目配置

  • 清空系统自带的配置文件
代码语言:shell
复制
> /usr/local/nginx/conf/nginx.conf
  • 编辑内容
代码语言:shell
复制
vim /usr/local/nginx/conf/nginx.conf
  • 添加内容如下
代码语言:shell
复制
#设置Nginx运行用户和用户组
user nobody nobody; 
#设置工作进程的数量。通常设置为CPU核心数,但也可以设置为更高的值以增加并发处理能力
worker_processes 2;                                                              #设置错误日志的路径和日志级别,并且只记录critical级别的错误
error_log /usr/local/nginx/logs/nginx_error.log crit;                            
#设置Nginx主进程的PID文件的路径。
pid /usr/local/nginx/logs/nginx.pid;
#设置每个工作进程可以打开的最大文件描述符数量
worker_rlimit_nofile 51200;                                                                                                                                        events
{
	#使用epoll事件模型
	use epoll;
	#置每个工作进程可以处理的最大连接数
	worker_connections 6000;
} 
http
{
	include mime.types;
	default_type application/octet-stream;
	server_names_hash_bucket_size 3526;
	server_names_hash_max_size 4096;
	log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
	'$host "$request_uri" $status'
	'"$http_referer" "$http_user_agent"';
	sendfile on;
	tcp_nopush on;
	keepalive_timeout 30;
	client_header_timeout 3m;
	client_body_timeout 3m;
	send_timeout 3m;
	connection_pool_size 256;
	client_header_buffer_size 1k;
	large_client_header_buffers 8 4k;
	request_pool_size 4k;
	output_buffers 4 32k;
	postpone_output 1460;
	client_max_body_size 200m;
	client_body_buffer_size 256k;
	client_body_temp_path /usr/local/nginx/client_body_temp;
	proxy_temp_path /usr/local/nginx/proxy_temp;
	fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
	fastcgi_intercept_errors on;
	tcp_nodelay on;
	gzip on;
	gzip_min_length 1k;
	gzip_buffers 4 8k;
	gzip_comp_level 5;
	gzip_http_version 1.1;
	gzip_vary on;
	gzip_types text/plain text/css application/json application/x-javascript application/javascript text/xml application/xml application/rss+xml text/javascript image/svg+xml application/vnd.ms-fontobject application/x-font-ttf font/opentype;
	server 
	{
		listen       80;
		server_name 你自己的服务器地址IP;
		location /admin 
		{
			alias    /var/www/html/admin/dist/;
			try_files $uri $uri/ /admin/index.html;
			add_header Cache-Control no-cache;
			index    index.html;
		}
		location /api 
		{
			#rewrite /api/(.*) /$1  break;
			proxy_pass      http://127.0.0.1:9999;
			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_set_header X-Forwarded-Proto $scheme;
		} 
		
		location / 
	    { 
		    root    /var/www/html/admin/dist/;
			try_files $uri $uri/ /index.html;
			add_header Cache-Control no-cache;
	    }
    }                                                                            
    include /usr/local/nginx/conf/host/*.conf;                               
}
  • 授权/var/www/html 目录给nobody
代码语言:shell
复制
sudo chown -R nobody:nobody /var/www/html
  • 注意: 1、 上述内容中 server_name 你自己的服务器地址IP; 需要替换为自己的IP 2、上述内容中proxy_pass http://127.0.0.1:9999; 仅适用于nginx 和服务端在同一台服务器的场景

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1、安装相关依赖
    • 2、下载并解压安装包
    • 3、配置并安装
      • 3.1 配置
      • 3.2 编译安装
    • 4、项目配置
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档