一、环境准备
yum -y install gcc pcre-devel zlib-devel openssl openssl-devel
二、Nginx安装包准备
Nginx下载地址:https://nginx.org/download/,完事上传至CentOS;
三、解压
tar -zxvf ./nginx-1.8.0.tar.gz -C /usr/local/
四、配置
./configure --prefix=/usr/local/nginx
五、安装
make & make install
六、测试
cd /usr/loca/nginx/
./sbin/nginx -t
七、启动
cd /usr/loca/nginx/sbin
./nginx
# 配置开机启动
vi /etc/rc.d/rc.local
八、问题解决
1)nginx: [error] invalid PID number "" in "/usr/local/nginx/logs/nginx.pid"
ps aux|grep nginx
cat /usr/local/nginx/logs/nginx.pid
echo 66769 > /usr/local/nginx/logs/nginx.pid
# 重启
nginx -s reload
*********************************************************************************************************
Nginx案例配置:
#user nobody;
# 全局块 设置部分影响Nginx服务器整体运行的配置指令
# 并发处理服务的配置,值越大,可以支持的并发处理量越多,但是会受到硬件、软件等设备的制约;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
# 影响Nginx服务器与用户的网络连接,常用的设置包括是否开启对多workprocess下的网络连接进行序列化,是否允许同时接收多个网络连接等;
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;
# MyServer
upstream myserver {
server 49.233.87.229:8080;
server 49.233.87.229:7070;
}
#gzip on;
# 1、示例(一)
# server {
# #*********************************************************************#
# # Port\IP 域名对应的IP/Port
# listen 80;
# server_name 127.0.0.1;
# #*********************************************************************#
#
# #charset koi8-r;
#
# #access_log logs/host.access.log main;
#
# #*********************************************************************#
# location / {
# root html;
# # 被代理的IP:Port
# proxy_pass http://49.233.87.229:8080;
# 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;
# #}
# }
# 2、示例(二)
#server {
# listen 80;
# server_name 127.0.0.1;
#
# location /edu/ {
# root html;
# proxy_pass http://49.233.87.229:8080;
# }
# location /vod/ {
# root html;
# proxy_pass http://49.233.87.229:7070;
# }
#}
# 3、示例(三) 常用效果;
#server {
# listen 80;
# server_name 127.0.0.1;
#
# location / {
# root html;
# proxy_pass http://myserver;
# index index.html index.htm;
# }
#}
# 4、示例(三) 动静分离;
server {
listen 80;
server_name 127.0.0.1;
location /www/ {
root /data/;
proxy_pass http://49.233.87.229:8080;
index index.html index.htm;
}
location /image/ {
# root后的路径是从根目录起始的; /data/image/
root /data/;
# proxy_pass http://49.233.87.229:8080;
autoindex on;
}
}
# 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;
# }
#}
}
解决Nginx负载均衡时,项目页面无法加载图片、CSS等样式的问题配置:
#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;
# Maxkey Server
upstream maxkeyserver {
server 172.16.104.162:9090;
server 172.16.104.109:80;
}
# Maxkey-mgt Server
upstream maxkeymgtsvr {
server 172.16.104.162:9521;
server 172.16.104.109:9521;
}
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
# ********************************************************************* #
location /maxkey/login/ {
# root html;
proxy_pass http://maxkeyserver;
# index index.html index.htm;
}
location ~ .* {
proxy_pass http://maxkeyserver;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
# ********************************************************************* #
#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;
#}
}
server {
listen 8000;
listen localhost;
# ********************************************************************* #
location /maxkey-mgt/login/ {
# root html;
proxy_pass http://maxkeymgtsvr;
# index index.html index.htm;
}
location ~ .* {
proxy_pass http://maxkeymgtsvr;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
# ********************************************************************* #
}
# 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;
# }
#}
}