未加星标

Linux环境下安装Nginx及其使用

字体大小 | |
[运维安全 所属分类 运维安全 | 发布者 店小二05 | 时间 | 作者 红领巾 ] 0人收藏点击收藏

一、查看CentOS的版本

cat /etc/redhat-release

二、添加资源库

在 CentOS 系统上安装 Nginx ,得先去添加一个资源库:

vim /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1

三、安装nginx

yum -y install nginx
Linux环境下安装Nginx及其使用

四、测试nginx配置文件是否正确

nginx -t

打印如下:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

五、CentOS7.0+ nginx实现停止、启动、重启

systemctl stop nginx.service
systemctl start nginx.service
systemctl restart nginx.service
systemctl status nginx.service
开机自启:

systemctl enable nginx.service
取消开机自启:

systemctl disable nginx.service

六、nginx.conf配置示例

我的配置:

user nginx;
worker_processes 4;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
#设定负载均衡的服务器列表
upstream pay_server {
ip_hash;
server 10.74.248.01:8088 max_fails=2 fail_timeout=2;
server 10.74.248.02:8088 max_fails=2 fail_timeout=2;
}
upstream print_server {
ip_hash;
server 10.74.248.03:2001 max_fails=2 fail_timeout=2;
server 10.74.248.04:2001 max_fails=2 fail_timeout=2;
}
upstream accss_door_server {
ip_hash;
server 10.74.248.05:2002 max_fails=2 fail_timeout=2;
server 10.74.248.06:2002 max_fails=2 fail_timeout=2;
}

server {
listen 80;
server_name wab.kupu.ccoop.com.cn;
location /invengo {
proxy_pass http://pay_server;
index index.jsp index.html index.htm;
}
location /invengo/epc {
proxy_pass http://print_server;
index index.jsp index.html index.htm;
}
location /invengo/print {
proxy_pass http://print_server;
index index.jsp index.html index.htm;
}
location /checkout {
proxy_pass http://accss_door_server;
index index.jsp index.html index.htm;
}
location /asset {
proxy_pass http://accss_door_server;
index index.jsp index.html index.htm;
}
}

常用的nginx.conf配置:

user www www;
worker_processes 2;
error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
pid logs/nginx.pid;
events {
use epoll;
worker_connections 2048;
}
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 65;
# gzip压缩功能设置
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 6;
gzip_types text/html text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml;
gzip_vary on;

# http_proxy 设置
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 75;
proxy_send_timeout 75;
proxy_read_timeout 75;
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 1 2;
# 设定负载均衡后台服务器列表
upstream backend {
#ip_hash;
server 192.168.10.100:8080 max_fails=2 fail_timeout=30s ;
server 192.168.10.101:8080 max_fails=2 fail_timeout=30s ;
}
# 很重要的虚拟主机配置
server {
listen 80;
server_name itoatest.example.com;
root /apps/oaapp;
charset utf-8;
access_log logs/host.access.log main;
#对 / 所有做负载均衡+反向代理
location / {
root /apps/oaapp;
index index.jsp index.html index.htm;
proxy_pass http://backend;
proxy_redirect off;
# 后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
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_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;

}
#静态文件,nginx自己处理,不去backend请求tomcat
location ~* /download/ {
root /apps/oa/fs;

}
location ~ .*\.(gif|jpg|jpeg|bmp|png|ico|txt|js|css)$
{
root /apps/oaapp;
expires 7d;
}
location /nginx_status {
stub_status on;
access_log off;
allow 192.168.10.0/24;
deny all;
}
location ~ ^/(WEB-INF)/ {
deny all;
}
#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;
}
}
## 其它虚拟主机,server 指令开始
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。


您可能感兴趣的文章:linux下nginx编译安装教程和编译参数详解Linux下安装配置nginx详解Linux下yum安装nginx教程在linux系统下安装两个nginx的简单方法linux下安装nginx(图文教程)Linux上安装搭建Nginx服务器的详细步骤linux下Nginx 0.8.40的安装方法Linux 安装nginx服务器详细介绍

本文运维安全相关术语:linux服务器代维 linux服务器搭建 运维管理 运维工程师 企业安全文章 企业安全管理 cf安全系统检测到游戏数据异常

tags: nginx,index,proxy,http,log,timeout,html,location,error,max,fails,fail
分页:12
转载请注明
本文标题:Linux环境下安装Nginx及其使用
本站链接:http://www.codesec.net/view/573585.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 运维安全 | 评论(0) | 阅读(221)