centos install nginx
部署Nginx
1.安装依赖
1 SSL功能需要openssl库,直接通过yum安装: yum install openssl
2 gzip模块需要zlib库,直接通过yum安装: yum install zlib
3 rewrite模块需要pcre库,直接通过yum安装: yum install pcre
2.使用yum安装nginx需要包括Nginx的库,安装Nginx的库
rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
3.安装nginx
yum install -y nginx
4.开机启动并启动Nginx
1.systemctl enable nginx.service
2.service nginx start
docker install
- docker pull nginx
常见操作
- 查看Nginx 运行及目录
ps -ef | grep nginx
2.验证配置是否正确:
nginx -t
3.查看Nginx的版本号
nginx -V
4.启动Nginx
start nginx 或 systemctl start nginx
或者
nginx -c /etc/nginx/nginx.conf
5.快速停止或关闭Nginx
nginx -s stop
6.正常停止或关闭Nginx
nginx -s quit
7.配置文件修改重装载命令
nginx -s reload
8.查看nginx 配置文件是否正确
nginx -t
9.查看nginx 安装目录
find /|grep nginx.conf
- 查找Nginx下的那个配置
sudo grep -r missdata.apexworkflow.com /etc/nginx
nginx 配置调整
1.修改运行cpu 保证以计算机的型号运行
2.配置nginx 集群
upstream dynamic {
server 172.16.2.108:8001;
server 172.16.2.109:8008;
server 172.16.2.110:8009;
}
server {
listen 8000;
server_name localhost;
#root /opt/sites/Caad.Dispatcher.CoreUI;
#index index.html;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location /{
proxy_pass http://dynamic;
proxy_http_version 1.1;
proxy_next_upstream error timeout invalid_header http_500 http_404 http_403;
proxy_connect_timeout 2;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
Nginx 一般配置
1.新建自定义配置文件
2.添加如下配置
server {
listen 80;
server_name localhost;
root /home/website/undocker/Caad.Dispatcher.CoreUI-Release;
index index.html;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location /{
#proxy_pass http://localhost:8100;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
vue 发布在Nginx的配置
server {
listen 9012;
server_name *****.com;
root /var/www/dist;
index index.html;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /var/www/dist;
index index.html;
try_files $uri $uri/ @router;
}
location @router {
rewrite ^.*$ /index.html last;
}
}
一些优化配置
#PROXY-START/
location /
{
expires 12h;
if ($request_uri ~* "(php|jsp|cgi|asp|aspx)")
{
expires 0;
}
proxy_pass http://192.168.9.78:8080;
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 REMOTE-HOST $remote_addr;
#持久化连接相关配置
#proxy_connect_timeout 30s;
#proxy_read_timeout 86400s;
#proxy_send_timeout 30s;
#proxy_http_version 1.1;
#proxy_set_header Upgrade $http_upgrade;
#proxy_set_header Connection "upgrade";
add_header X-Cache $upstream_cache_status;
#Set Nginx Cache
add_header Cache-Control no-cache;
}
location /api{
add_header 'Access-Control-Allow-Origin' $http_origin;
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,web-token,app-token,Authorization,Accept,Origin,Keep-Alive,User-Agent,X-Mx-ReqToken,X-Data-Type,X-Auth-Token,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain; charset=utf-8';
add_header 'Content-Length' 0;
return 204;
}
proxy_pass http://192.168.9.78:8081;
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;
proxy_connect_timeout 5;
#持久化连接相关配置
#proxy_connect_timeout 30s;
#proxy_read_timeout 86400s;
#proxy_send_timeout 30s;
#proxy_http_version 1.1;
#proxy_set_header Upgrade $http_upgrade;
#proxy_set_header Connection "upgrade";
add_header X-Cache $upstream_cache_status;
#Set Nginx Cache
add_header Cache-Control no-cache;
}
#PROXY-END/
常见问题
提示pid 找不到
执行 nginx -c /etc/nginx/nginx.conf
如果不行,把Nginx杀死
只是显示nginx 默认页面
看看是否是端口被占用了,修改监听端口
外网跳转到内网,内网不能访问
如果外网监听80,内部部署Nginx监听又是80,会有问题
最好是自定义一个端口进行监听