一、Nginx下载
下载地址: http://nginx.org/en/download.html
或Linux系统上 wget http://nginx.org/download/nginx-1.15.8.tar.gz 可以直接下载
或Linux系统上 yum list nginx 查看可以下载的版本
安装Nginx前需要安装 PCRE
下载地址: wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.40.tar.gz
二、Nginx安装
使用root用户登录Linux系统
通过命令 whereis nginx 或者 ps -ef|grep nginx 查看 是否已经安装了nginx软件
安装PCRE
找到下载的 pcre-8.40.tar.gz 包
通过命令 tar -xvf pcre-8.40.tar.gz 解压缩
执行命令 cd pcre-8.40
执行命令 ./configure
检查安装前的配置需要,如果有错误,依次解决完错误后,再继续这一步;
如果没有问题或问题解决完成后,接着执行命令:
make && make install
来完成安装
完整的过程如下:
[root@localhost config]# tar -xvf pcre-8.40.tar.gz
安装内容...
[root@localhost config]# cd pcre-8.40
[root@localhost pcre-8.40]# ./configure
检查内容...
[root@localhost pcre-8.40]# make && make install
安装内容...
安装Nginx
整个安装的过程,跟PCRE类似
如下:
[root@localhost config]# tar -xvf nginx-1.15.8.tar.gz
安装内容...
[root@localhost config]# cd nginx-1.15.8
[root@localhost pcre-8.40]# ./configure
检查内容...
[root@localhost pcre-8.40]# make && make install
安装内容...
以上步骤如果不报错误,则表示安装成功,安装出错的话,解决错误;
三、启动
先查找Nginx被默认安装到了什么位置上
通过命令 whereis nginx
启动: /usr/local/nginx/sbin/nginx
重载: /usr/local/nginx/sbin/nginx -s reload
停止: /usr/local/nginx/sbin/nginx -s stop
四、配置文件
########### 每个指令必须有分号结束。#################
#user administrator administrators; #配置用户或者组,默认为nobody nobody。
#worker_processes 2; #允许生成的进程数,默认为1
#pid /nginx/pid/nginx.pid; #指定nginx进程运行文件存放地址
error_log log/error.log debug; #制定日志路径,级别。这个设置可以放入全局块,http块,server块,级别以此为:debug|info|notice|warn|error|crit|alert|emerg
events {
accept_mutex on; #设置网路连接序列化,防止惊群现象发生,默认为on
multi_accept on; #设置一个进程是否同时接受多个网络连接,默认为off
#use epoll; #事件驱动模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport
worker_connections 1024; #最大连接数,默认为512
}
http {
include mime.types; #文件扩展名与文件类型映射表
default_type application/octet-stream; #默认文件类型,默认为text/plain
#access_log off; #取消服务日志
log_format myFormat '$remote_addr–$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for'; #自定义格式
access_log log/access.log myFormat; #combined为日志格式的默认值
sendfile on; #允许sendfile方式传输文件,默认为off,可以在http块,server块,location块。
sendfile_max_chunk 100k; #每个进程每次调用传输数量不能大于设定的值,默认为0,即不设上限。
keepalive_timeout 65; #连接超时时间,默认为75s,可以在http,server,location块。
upstream mysvr {
server 127.0.0.1:7878;
server 19.16.1.11:3333 backup; #热备
}
error_page 404 https://www.baidu.com; #错误页
server {
keepalive_requests 120; #单连接请求上限次数。
listen 4545; #监听端口
server_name 127.0.0.1; #监听地址
location ~*^.+$ { #请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小写。
#root path; #根目录
#index vv.txt; #设置默认页
proxy_pass http://mysvr; #请求转向mysvr 定义的服务器列表
deny 127.0.0.1; #拒绝的ip
allow 12.18.5.4; #允许的ip
}
}
}
本人示例:
#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 {
accept_mutex on;
multi_accept on;
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#access_log off;
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;
# My Zentao agent server
upstream my_zentao {
server 127.0.0.1:8085;
}
upstream my_cibs {
server 127.0.0.1:8081 weight=1;
}
server {
listen 80;
server_name www.abcd.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://my_cibs/cibs/;
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;
access_log logs/cibs/my_cibs.log main;
}
location /zentao {
proxy_pass http://my_zentao/zentao/;
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;
access_log logs/zentao/my_zentao.log main;
}
location /cibs {
proxy_pass http://my_cibs/cibs/;
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;
access_log logs/cibs/my_cibs.log main;
}
#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;
# }
#}
}
感谢博主:
https://www.cnblogs.com/knowledgesea/p/5175711.html
至此结束,谢谢!