Centos7安装和配置Nginx服务器
一、安装编译工具及库文件
yum -y install gcc gcc-c++ autoconf automake make libtool
yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel
二、首先要安装 PCRE
PCRE 作用是让 Nginx 支持 Rewrite 功能
首先查看是否已经安装
pcre-config --version 查看版本号,查到这说明已安装,可以跳过PCRE的安装步骤。
1、下载 PCRE 安装包,下载地址: http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
[root@bogon src]# cd /usr/local/src/
[root@bogon src]# wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
2、解压安装包:
[root@bogon src]# tar zxvf pcre-8.35.tar.gz
3、进入安装包目录
[root@bogon src]# cd pcre-8.35
4、编译安装
[root@bogon pcre-8.35]# ./configure
[root@bogon pcre-8.35]# make && make install
5、查看pcre版本
[root@bogon pcre-8.35]# pcre-config --version
三、安装 Nginx
1、下载 Nginx
https://nginx.org/download/nginx-1.18.0.tar.gz
直接下载后上传到 /usr/local/src目录下
或者
Wget https://nginx.org/download/nginx-1.18.0.tar.gz
2、解压安装包
[root@IntPlan-test-app src]# tar zxvf nginx-1.18.0.tar.gz
3、进入安装包目录
[root@IntPlan-test-app src]# cd /usr/local/src/nginx-1.8.0
4、编译安装
默认安装
[root@IntPlan-test-app nginx-1.18.0]# ./configure
【也可以用下面的语句自定义安装】
[root@IntPlan-test-app nginx-1.18.0]# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.32
不知道pcre版本可以通过,pcre-config --version 或下面的命令查看本机pcre版本
rpm -qa pcre
结果: pcre-8.32-17.el7.x86_64
[root@IntPlan-test-app nginx-1.18.0]# make
[root@IntPlan-test-app nginx-1.18.0]# sudo make install
5、查看nginx版本
查看版本:
[root@IntPlan-test-app nginx-1.18.0]# /usr/local/nginx/sbin/nginx -v
nginx version: nginx/1.18.0
安装完毕。
四、Nginx 配置
创建 Nginx 运行使用的用户 www:
[root@IntPlan-test-slb conf]# /usr/sbin/groupadd www
[root@IntPlan-test-slb conf]# /usr/sbin/useradd -g www www
配置nginx.conf ,将/usr/local/nginx/conf/nginx.conf替换为以下内容
user www;
worker_processes 4;#ÉèÖÃÖµºÍCPUºËÐÄÊýÒ»Ö»ò2±¶
error_log /usr/local/nginx/logs/nginx_error.log crit;#ÈÕ־λÖúÍÈÕÖ¾¼¶±ð
pid /usr/local/nginx/nginx.pid;
worker_rlimit_nofile 65535;
events{
use epoll;
worker_connections 65535;
}
http{
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr -
r
e
m
o
t
e
u
s
e
r
[
remote_user [
remoteuser[time_local] “KaTeX parse error: Double superscript at position 34: … '̲status
b
o
d
y
b
y
t
e
s
s
e
n
t
"
body_bytes_sent "
bodybytessent"http_referer” ’
‘“
h
t
t
p
u
s
e
r
a
g
e
n
t
"
"
http_user_agent" "
httpuseragent""http_x_forwarded_for”’;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 256m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
#limit_zone crawler KaTeX parse error: Expected 'EOF', got '#' at position 25: …mote_addr 10m; #̲ÏÂÃæÊÇserverÐéÄ…
{
#fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
location ~ .*.(gif|jpg|jpeg|png|bmp|swf|ico)$
{
expires 30d;
access_log off;
}
location ~ .*.(js|css)?$
{
expires 15d;
#access_log off;
}
access_log off;
}
}
检查配置文件nginx.conf的正确性命令:
五、启动 Nginx
Nginx 启动命令如下:
[root@IntPlan-test-slb conf]# /usr/local/nginx/sbin/nginx
master为主进程,worker工作线程
停止服务
[root@IntPlan-test-slb conf]# /usr/local/nginx/sbin/nginx -s stop
重启服务
[root@IntPlan-test-slb conf]# /usr/local/nginx/sbin/nginx -s reopen
重新载入配置文件
[root@IntPlan-test-slb conf]# /usr/local/nginx/sbin/nginx -s reload
六、访问站点
从浏览器访问我们配置的站点ip:
http://10.190.202.149/
访问失败,查看防火墙是否开启
[root@IntPlan-test-slb ~]# systemctl status firewalld.service
关闭防火墙
再次连接
访问成功,说明防火墙没有开启80端口。
开启防火墙80端口
[root@IntPlan-test-slb ~]# firewall-cmd --add-port=80/tcp --permanent
然后重启防火墙
[root@IntPlan-test-slb ~]# systemctl restart firewalld.service
就可以正常访问了。
**
Nginx 配置详情
打开nginx安装目录下
/usr/local/nginx/conf/nginx.conf
用下面的内容替换掉nginx.conf中的内容,然后重启nginx
#使用的用户和组
user www www;
#指定工作衍生进程数(一般是cpu总核数或其2倍)
worker_processes auto;
#指定 pid存放路径
pid /var/run/nginx.pid;
#log location and level[ debug | info | notice | warn | error | crit ]
error_log /var/log/nginx/error.log crit;
指定文件描述符数量
worker_rlimit_nofile 65535;
events {
#允许的连接数
worker_connections 2000;
# 使用的网络I/O模型,Linux系统推进epoll,FreeBSD推荐kqueue
# use [ kqueue | epoll | /dev/poll | select | poll ];
use epoll;
}
http {
include mime.types;
default_type application/octet-stream;
#日志格式 http_x_forwarded_for用于在反向代理用记录用户真实的ip地址
log_format main '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" '
'"$gzip_ratio" "$http_x_forwarded_for"';
log_format download '$remote_addr - $remote_user [$time_local] '
'"$request" $status $bytes_sent '
'"$http_referer" "$http_user_agent" '
'"$http_range" "$sent_http_content_range" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
#设置字符集,如果一个网站有多种字符集,请不要随便设置,应该在html代码中通过meta标签设置
#charset gb2312;
# 读取客户端请求标头的超时。如果客户端在此时间内不发送整个请求头,则请求终止(408)(请求超时)错误
client_header_timeout 3m;
#客户端与服务端建立连接后发送 request body 的超时时间
client_body_timeout 3m;
#客户端能够上传的文件大小
# 客户端请求主体的最大允许大小
client_max_body_size 512m;
#服务端向客户端传输数据的超时时间
send_timeout 3m;
server_names_hash_bucket_size 128;
# 设置用于读取客户端请求头的缓冲区大小
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
# 是否允许header name中含有下划线_在http请求头的头部中设置了一些自定义字段
# 比如bundle_name这种,后端在进去获取头部信息时,发现取不到对应的值,改为on才可以
# 允许header name中含有下划线_
underscores_in_headers on;
#开启gzip压缩
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
#gzip_http_version 1.0;
#gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
#gzip_vary on;
output_buffers 1 32k;
postpone_output 1460;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 60;
#send_lowat 12000;
# 支持php配置
#fastcgi_connect_timeout 300;
#fastcgi_send_timeout 300;
#fastcgi_read_timeout 300;
#fastcgi_buffer_size 64k;
#fastcgi_buffers 4 64k;
#fastcgi_busy_buffers_size 128k;
#fastcgi_temp_file_write_size 128k;
#lingering_time 30;
#lingering_timeout 10;
#关闭不响应的客户端连接
#reset_timedout_connection on;
# 线程池大小 1.9version前32平台默认256,64平台默认512
#connection_pool_size 512;
#虚拟主机1配置
server {
#监听的ip和端口 不写ip则表示监听本机所有ip,通过server_name区分 listen 10.190.202.149:80;
listen 80 default_server;
listen [::]:80 default_server;
# 主机名称 ip地址或者域名
server_name 10.190.202.149;
# server_name one.example.com www.one.example.com;
# 默认首页,顺序从左至右依次查找
index index index.html index.htm index.php;
# main 指采用log_format中名字为main的日志格式
# 访问日志文件存放的路径 access_log /var/log/nginx/$server_name.log main buffer=32k;
access_log /var/log/nginx/access.log main;
# 站点目录
root /opt/work/plandesign/design;
location / {
autoindex on;
# 代理配置
#proxy_pass http://127.0.0.1/;
#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_set_header Cookie "$http_cookie";
#proxy_hide_header X-Powered-By;
#proxy_hide_header X-AspNet-Version;
#proxy_hide_header X-AspNetMvc-Version;
#
client_max_body_size 10m;
# 设置用于读取客户端请求体的缓冲区大小
client_body_buffer_size 128k;
# 用于存储持有客户端请求主体的混存文件的目录
#client_body_temp_path /usr/local/nginx/client_body_temp;
proxy_connect_timeout 70;
proxy_send_timeout 90;
proxy_read_timeout 90;
#proxy_send_lowat 12000;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
#proxy_temp_path /usr/local/nginx/proxy_temp;
#charset koi8-r;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
location /old_stuff/ {
rewrite ^/old_stuff/(.*)$ /new_stuff/$1 permanent;
}
location /download/ {
valid_referers none blocked server_names *.example.com;
if ($invalid_referer) {
#rewrite ^/ http://www.example.com/;
return 403;
}
#rewrite_log on;
# rewrite /download/*/mp3/*.any_ext to /download/*/mp3/*.mp3
rewrite ^/(download/.*)/mp3/(.*)\..*$
/$1/mp3/$2.mp3 break;
root /spool/www;
#autoindex on;
access_log /var/log/nginx-download.access_log download;
}
location ~* \.(jpg|jpeg|gif|png|bmp|swf|ico)$ {
root /spool/www;
access_log off;
expires 30d;
}
location ~ .*\.(js|css)?$ {
expires 15d;
#access_log off;
}
#php 配置
#location ~ .*\.(php|php5)?$ {
# fastcgi_pass unix:/tmp/php-cgi.sock;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# include fastcgi.conf;
#}
}
#虚拟主机2
server {
listen 81;
server_name _;
root /opt/work/offshore/dist;
# Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
location / {
root /opt/work/offshore/dist;
#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 Cookie "$http_cookie";#proxy_hide_header X-Powered-By;
#proxy_hide_header X-AspNet-Version;
#proxy_hide_header X-AspNetMvc-Version;
}
location /SimpleViewApp/ {
proxy_pass http://190.10.10.137:8000;
}
}
}
重启Nginx服务后,访问