安装Nginx

当使用以下命令安装Nginx时,发现无法安装成功

yum install -y nginx 

需要做一点处理。

安装Nginx源

执行以下命令:

rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

安装该rpm后,我们就能在/etc/yum.repos.d/ 目录中看到一个名为nginx.repo 的文件。

安装Nginx

安装完Nginx源后,就可以正式安装Nginx了。

yum install -y nginx

Nginx默认目录

输入命令:

whereis nginx

即可看到类似于如下的内容:

nginx: /usr/sbin/nginx /usr/lib64/nginx /etc/nginx /usr/share/nginx

以下是Nginx的默认路径:

(1) Nginx配置路径:/etc/nginx/
(2) PID目录:/var/run/nginx.pid
(3) 错误日志:/var/log/nginx/error.log
(4) 访问日志:/var/log/nginx/access.log
(5) 默认站点目录:/usr/share/nginx/html

事实上,只需知道Nginx配置路径,其他路径均可在/etc/nginx/nginx.conf 以及/etc/nginx/conf.d/default.conf 中查询到。

常用命令

(1) 启动:

nginx

(2) 测试Nginx配置是否正确:

nginx -t

(3) 优雅重启:

nginx -s reload

(4) 查看nginx的进程号:

ps -ef |grep nginx

(5)nginx服务停止

nginx -s stop
kill -9 pid

当然,Nginx也可以编译源码安装,步骤相对要繁琐一些,总的来说,还是比较简单的,本文不作赘述

虚拟机配置

server {
listen 80;
server_name aaa.com;
rewrite ^(.*) http://www.aaa.com$1 permanent;
}

server {
listen 80;
server_name www.aaa.com;
index index.php index.html;
root /data/aaa/public;
location ~ \.php$ {
try_files $uri =404;
#fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}

location ~* \.(jpg|jpeg|gif|png|swf|rar|zip|css|ico|js|ico|cert|xml|txt)$ {
access_log off;
expires 30d;
break;
}

location / {
# !!!url重写!!!
if (!-e $request_filename) {
#rewrite ^/index.php(.*)$ /index.php?s=$1 last;
rewrite ^(.*)$ /index.php?s=$1 last;
break;
}
}
}



server {
  listen 80;
  server_name www.goreborn.com.cn;
  #配置强制转https
  rewrite ^(.*) https://$server_name$1 permanent;
}

server {
  listen 443 ssl;
  server_name www.goreborn.com.cn;
  ssl_certificate /etc/nginx/ssl_certs/3186178_www.goreborn.com.cn.pem;
  ssl_certificate_key /etc/nginx/ssl_certs/3186178_www.goreborn.com.cn.key;
  #ssl_session_cache shared:SSL:1m;
  ssl_session_timeout 5m;
  ssl_ciphers HIGH:!aNULL:!MD5;
  ssl_prefer_server_ciphers on;
  location /{
  proxy_pass http://127.0.0.1:8080;
  # 后端的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;

  add_header Access-Control-Allow-Origin *;
  proxy_set_header X-Forwarded-Proto https; #此处是https访问的
  关键环节
  proxy_redirect off;
  }
  location /basePath/ {
    alias /usr/local/soft/apache/apache-tomcat-8.5.31/;
    autoindex on;
  }

}

server {
        listen 80;
        server_name www.5568gy.com 5568gy.com;
        #配置强制转https
        rewrite ^(.*) https://www.5568gy.com$1 permanent;
}
server {
        listen 443 ssl;
        server_name www.5568gy.com;
        ssl_certificate /etc/nginx/ssl.d/3574221_5568gy.com.pem;
        ssl_certificate_key /etc/nginx/ssl.d/3574221_5568gy.com.key;
        #ssl_session_cache shared:SSL:1m;
        ssl_session_timeout 5m;
        ssl_ciphers HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers on;

        index index.php index.html;
        root /data/php-tianmiaoxing/public;
        location ~ \.php$ {
                try_files $uri =404;
                #fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;
                include /etc/nginx/fastcgi_params;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_param PATH_INFO $fastcgi_path_info;
        }
        location / {
                # !!!url重写!!!
                if (!-e $request_filename) {
                        rewrite ^/(.*)$ /index.php?s=$1 last;
                        #rewrite ^/index.php(.*)$ /index.php?s=$1 last;
                        break;
                }
        }
}



开启成功,将服务加入开机自启

systemctl enable nginx.service #注意后面不能跟空格

 systemctl enable php-fpm.service