Centos 7 配置Nginx,常用的nginx操作,启动/停止,代理,反向代理设置以及https ssl 443配置

Linux

查询nginx主进程号

ps -ef | grep nginx

启动/重启

## 在nginx/sbin下执行命令 . (查看是否在  /usr/local/nginx/sbin)
## 启动
./nginx -c /usr/local/nginx/conf/nginx.conf
## 重启
./nginx -s reload

停止

## 从容停止Nginx:
kill -QUIT 主进程号  
## 例如:kill -QUIT 16391

## 快速停止Nginx:
kill -TERM 主进程号  

## 强制停止Nginx:
kill -9 主进程号 

## 停止nginx
nginx -s stop

代理/请求转发

http {
    server {
            ### ...

            listen       4000;
            server_name  localhost;

            location / {
                root   /Users/zhangguoye/Documents/Porject/Gitee/searchWX/src/main/internetapp;
                index  index.html index.htm;
            }
    
            location /oauth/ {
                    proxy_pass http://localhost:8080/oauth/;
            }
    
            location /api/ {
                    proxy_pass http://localhost:8080/api/;
            }
            ### ...
    }
}

443/SSL/未开启SSL模块

安装模块

切换到源码包:

cd /usr/local/src/nginx-1.11.3

查看nginx原有的模块

/usr/local/nginx/sbin/nginx -V

在configure arguments:后面显示的原有的configure参数如下:

--prefix=/usr/local/nginx --with-http_stub_status_module

那么我们的新配置信息就应该这样写:

./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

运行上面的命令即可,等配置完

配置完成后,运行命令

makes

然后备份原有已安装好的nginx

cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak

然后将刚刚编译好的nginx覆盖掉原有的nginx(这个时候nginx要停止状态)

cp ./objs/nginx /usr/local/nginx/sbin/

然后启动nginx,仍可以通过命令查看是否已经加入成功

/usr/local/nginx/sbin/nginx -V

配置Http和Https共存

把ssl on;这行去掉,ssl写在443端口后面。这样http和https的链接都可以用

server {
            listen 80 default backlog=2048;
            listen 443 ssl;
            server_name wosign.com;
            root /var/www/html;
  
            ssl_certificate /usr/local/Tengine/sslcrt/ wosign.com.crt;
            ssl_certificate_key /usr/local/Tengine/sslcrt/ wosign.com .Key;
        }

配置SSL安全证书重启避免输入密码

可以用私钥来做这件事。生成一个解密的key文件,替代原来key文件。

openssl rsa -in server.key -out server.key.unsecure

SSL性能调优

ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES256-SHA384:AES256-SHA256:RC4:HIGH:!MD5:!aNULL:!eNULL:!NULL:!DH:!EDH:!AESGCM;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;

MAC (Brew Nginx)

mac 使用homebrew安装Nginx,Nginx的位置与启动

## 在mac上安装完nginx后的提示信息
==> nginx
Docroot is: /usr/local/var/www

The default port has been set in /usr/local/etc/nginx/nginx.conf to 8080 so that
nginx can run without sudo.

nginx will load all files in /usr/local/etc/nginx/servers/.

To have launchd start nginx now and restart at login:
  brew services start nginx
Or, if you don't want/need a background service you can just run:
  nginx
## 查看nginx版本
nginx -v

## 启动nginx服务
brew services start nginx

## 关闭nginx服务
brew services stop nginx

## 重新加载nginx
nginx -s reload

## 停止nginx
nginx -s stop

Guoye
318 声望9 粉丝