得到配置目录:./nginx -t
生成pid:./nginx -c /etc/nginx/nginx.conf
重启:./nginx -s reload
nginx.conf 主配置文件:
user root;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
******# Load dynamic modules. See /usr/share/nginx/README.dynamic.******
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
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 /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
include /etc/nginx/conf.d/*.conf;
}
**引入的/etc/nginx/conf.d/ 文件下的配置文件
main.conf:
server {
listen 80 ;
server_name xxx.com ;
root /usr/share/nginx/html;
#include /etc/nginx/default.d/*.conf;
#代理到8081应用
location / {
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://localhost:8081;
}
#配置静态资源
location /images/ {
root /root/frame/;
autoindex on;
}
#静态资源目录浏览
location /soft {
root /root/;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
二级域名配置:
second.conf
server
{
listen 80;
server_name aml.xxx.com;
#代理到8080应用
location / {
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://localhost:8080;
}
}
HTTPS配置:
1.重写HTTP请求转HTTPS
server{
listen 80;
server_name xxx.com;
rewrite ^/(.*)$ https://$server_name$1 permanent;
}
2.监听433端口,配置证书
server {
listen 443 ssl;
server_name xxx.com ;
root /usr/share/nginx/html;
ssl_certificate /root/ca/ca.pem;
ssl_certificate_key /root/ca/ca.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
location / {
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://localhost:8081;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
简单的文件服务器:
main.conf:
server {
listen 80 ;
server_name localhost ;
charset utf-8; # 避免中文乱码
location / {
root /usr/share/nginx/html;
}
}
server{
listen 8088 ;
server_name localhost ;
charset utf-8; # 避免中文乱码
root /root/files/;
#静态资源目录浏览
location / {
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
}
}
1.ps aux | grep nginx 查看nginx 路径
2.路径为:/usr/local/nginx/sbin/nginx ,执行/usr/local/nginx/sbin/nginx -t;检测是否配置错误
3./usr/local/nginx/sbin/nginx -s reload; 重载配置文件
or service nginx start|stop|restart